leetcode 134: Sort List

Sort List

Total Accepted: 453 Total Submissions: 2621

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



/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *sortList(ListNode *head) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        //merge sort
        return mergeSort(head);
    }
    
    ListNode* mergeSort(ListNode* head) {
        if(head==NULL || head->next==NULL) return head;
        //split
        ListNode* p=head; ListNode* q=head;
        ListNode* pre = p;
        while(q!=NULL && q->next!=NULL) {
            q=q->next->next;
            pre = p;
            p=p->next;
        }
        pre->next = NULL;
        ListNode* h1 = mergeSort(head);
        ListNode* h2 = mergeSort(p);
        return merge(h1,h2);
    }
    
    ListNode* merge(ListNode* h1, ListNode* h2) {
        ListNode* dump = new ListNode(0);
        ListNode* p = dump;
        while(h1!=NULL && h2!=NULL) {
            if(h1->val<h2->val){
                p->next = h1;
                h1 = h1->next;
            } else {
                p->next = h2;
                h2 = h2->next;
            }
            p = p->next;
        }
        
        if(h1!=NULL) p->next = h1;
        else p->next = h2;
        ListNode* head = dump->next;
        delete dump;
        return head;
    }
};


 /* Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode sortList(ListNode head) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        if(head==null || head.next==null) return head;
        
        ListNode p,q;
        p = q =head;
        ListNode pre=p;
        while(q!=null && q.next!=null) {
            q= q.next.next;
            pre=p;
            p = p.next;
        }
        pre.next = null;
        
        return merge( sortList(head), sortList(p));
    }
    
    private ListNode merge(ListNode h1, ListNode h2) {
        ListNode head = new ListNode(0);
        ListNode p = head;
        while(h1!=null && h2!=null) {
            if(h1.val<h2.val){
                p.next = h1;
                h1 = h1.next;
            } else {
                p.next = h2;
                h2 = h2.next;
            }
            p = p.next;
        }    
        p.next = h1!=null? h1 : h2;
        return head.next;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值