148. Sort List(归并排序)

148. Sort List


 

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

 

利用mergesort 

merge 操作只能合并2个有序的子序列

所以利用递归对每个子序列进行排序,排序后merge。

 

 

 

 

 

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        
        def merge(node1,node2):
            fake = ListNode(-1)
            cur = fake
            while node1 != None and node2 != None:
                if node1.val < node2.val:
                    cur.next = node1
                    node1 = node1.next
                else:
                    cur.next = node2
                    node2 = node2.next
                cur = cur.next
            if node1 != None:
                cur.next = node1
            if node2 != None:
                cur.next = node2
            return fake.next
        
        if head == None or head.next == None:
            return head
        slow = fast = head
        fake = ListNode(0,head)
        slow = fake
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next

        node1 = head
        node2 = slow.next
        slow.next = None
        node1 = self.sortList(node1)
        node2 = self.sortList(node2)
        return merge(node1,node2)

        

 

 

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* merge(ListNode* headA, ListNode* headB) {
        ListNode* fakehead = new ListNode(0);
        ListNode* cur = fakehead;

        while(headA!=nullptr && headB != nullptr) {
           if (headA->val<headB->val) {
                cur->next = headA;
                headA = headA->next;
           } else {
                cur->next = headB;
                headB = headB->next;
           }
           cur = cur->next;
        }
        cur->next = headA == nullptr?headB:headA;
        return fakehead->next;
    }

    ListNode* midnode(ListNode* head) {
        if (head == nullptr||head->next == nullptr ) return head;
        ListNode* slow = head;
        ListNode* fast = head;
        ListNode* slow_pre =new ListNode(0);
        slow_pre->next = slow;
        while(fast!= nullptr && fast->next!=nullptr) {
            slow = slow->next;
            fast = fast->next->next;
            slow_pre = slow_pre->next;
        }
        return slow_pre;       
    }
    ListNode* merge_sort(ListNode* head) {
        if (head == nullptr||head->next == nullptr ) return head;
        ListNode*  mid = midnode(head);
        ListNode* headA = head;
        ListNode* headB = mid->next;
        mid->next = nullptr;

        ListNode* sortheadA = merge_sort(headA);
        ListNode* sortheadB = merge_sort(headB);
        return merge(sortheadA,sortheadB);
    }
    ListNode* sortList(ListNode* head) {
        return merge_sort(head);
    }
};

 

 1 class Solution {
 2     public ListNode sortList(ListNode head) {
 3         if(head==null || head.next==null) return head;
 4         
 5         //1 split 
 6         ListNode pre=null,slower =head,faster=head;
 7         while(faster!=null && faster.next!= null){
 8             pre = slower;
 9             slower = slower.next;
10             faster = faster.next.next;
11         }
12         pre.next = null;
13         //2 sort 
14         ListNode l1 = sortList(head);
15         ListNode l2 = sortList(slower);
16         //3 merge
17         return Merge(l1,l2);
18         
19     }
20     
21     public ListNode Merge(ListNode l1,ListNode l2){
22         ListNode l ,p;
23         l = new ListNode(0);
24         p=l;
25         
26         while(l1 != null&& l2 != null){
27             if(l1.val<l2.val){
28                 p.next = l1;
29                 l1 = l1.next;
30             }
31             else{
32                 p.next = l2;
33                 l2 = l2.next;
34             }
35             p = p.next;
36         }
37         if(l1==null) p.next = l2;
38         if(l2==null) p.next = l1;
39         return l.next;
40     }
41 }

 

posted @ 2017-10-09 20:51  乐乐章  阅读(149)  评论(0编辑  收藏  举报