Insertion Sort List,Merge Two Sorted Lists,Sort List

Insertion Sort List
Sort a linked list using insertion sort.node

1.解題思路spa

題目很簡單,就是要求用插入排序的方法來爲鏈表排序。插入排序就是每次遍歷一個新的元素,將其插入到前面已經排好序的元素中。
由於頭結點無法肯定,因此咱們用一個dummy節點;而後開始向後逐個遍歷,當前遍歷的節點爲cur,另外sortnode每次都指向已經排好序的第一個節點dummy,而後從dummy開始日後,直到找到sortnode.next.val>=cur.val,則表示cur節點要插到有序鏈表的sortnode後面。指針

2.代碼code

public class Solution {
    public ListNode insertionSortList(ListNode head) {
        if(head==null||head.next==null) return head;
        ListNode dummy=new ListNode(0);
        ListNode cur=head;
        while(cur!=null){
            ListNode sortnode=dummy;
            while(sortnode.next!=null&&sortnode.next.val<cur.val)
                sortnode=sortnode.next;
            ListNode tmp=cur.next;
            cur.next=sortnode.next;
            sortnode.next=cur;
            cur=tmp;
        }
        return dummy.next;
    }
}

Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.排序

1.解題思路遞歸

合併兩個有序鏈表,一樣須要構造一個Dummy節點。
1)考慮特殊狀況,若是有一個鏈表爲空,則直接返回另外一個連接;
2)利用兩個指針p,q遍歷兩個鏈表,若是都不爲空,則循環繼續;
3)使用node指向新鏈表的最後一個節點,初始化爲dummy
4) 比較p,q的數值的大小,若是p小於q,則把p加到node.next,而且node賦值爲p,而p指針須要前進一個;
5) 結束循環時,check一下是否還有鏈表中有剩餘元素,並將剩餘元素加入到新鏈表node指針的後面。ci

2.代碼get

public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null) return l2;
        if(l2==null) return l1;
        ListNode dummy=new ListNode(0);
        ListNode p=l1;
        ListNode q=l2;
        ListNode node=dummy;
        while(p!=null&&q!=null){
            if(p.val<q.val){
                node.next=p;
                node=p;
                p=p.next;
                
            }
            else {
                node.next=q;
                node=q;
                q=q.next;
            }
        }
        if(p!=null)
            node.next=p;
        if(q!=null)
            node.next=q;
        return dummy.next;
    }
}

Sort Listit

Sort a linked list in O(n log n) time using constant space complexity.io

1.解題思路

題目要求時間複雜度爲O(n log n),因此咱們就想到用歸併排序,天然就想到和上一題的mergeTwoLists。
但要作mergeTwoLists,必須得先將當前的list分割成兩個List,因此採用遞歸實現。
要分割鏈表,最簡單的辦法就是找到中點,那很明顯是利用slow,fast來實現,最後slow就是鏈表的中間點。但要注意咱們要將Slow的前一個節點記錄下來pre,在找到中點後,咱們要將pre.next=null,這樣鏈表才能分割成2個。
2.代碼

public class Solution {
    public ListNode sortList(ListNode head) {
        if(head==null||head.next==null) return head;
        ListNode slow=head,fast=head,pre=null;
        while(fast!=null&&fast.next!=null){
            pre=slow;
            slow=slow.next;
            fast=fast.next.next;
        }
        pre.next=null;
        ListNode l1=sortList(head);
        ListNode l2=sortList(slow);
        return mergeTwoSortedList(l1,l2);
    }
    private ListNode mergeTwoSortedList(ListNode l1,ListNode l2){
        if(l1==null) return l2;
        if(l2==null) return l1;
        ListNode dummy=new ListNode(0);
        ListNode p=l1;
        ListNode q=l2;
        ListNode node=dummy;
        while(p!=null&&q!=null){
            if(p.val<q.val){
                node.next=p;
                node=p;
                p=p.next;
                
            }
            else {
                node.next=q;
                node=q;
                q=q.next;
            }
        }
        if(p!=null)
            node.next=p;
        if(q!=null)
            node.next=q;
        return dummy.next;
    }
}
相關文章
相關標籤/搜索