1. 程式人生 > >lintcode,連結串列排序

lintcode,連結串列排序

在 O(n log n) 時間複雜度和常數級的空間複雜度下給連結串列排序。
樣例
給出 1->3->2->null,給它排序變成 1->2->3->null.

解題思路:根據要求採用先sort再merge的方法,首先找到中點,然後從中點兩端分別sort,將兩個結果進行merge。

一刷ac
歸併思路實現

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next
= null; * } * } */ public class Solution { /** * @param head: The head of linked list. * @return: You should return the head of the sorted linked list, using constant space complexity. */ public ListNode sortList(ListNode head) { if(head == null
|| head.next == null) return head; ListNode mid = findMid(head); ListNode tmp = mid.next; mid.next = null; ListNode headleft = sortList(head); ListNode headright = sortList(tmp); return merge(headleft, headright); } public static ListNode findMid(ListNode head){ if
(head == null || head.next == null) return head; ListNode slow = head; ListNode fast = head; while(fast.next != null && fast.next.next != null){ fast = fast.next.next; slow = slow.next; } return slow; } public static ListNode merge(ListNode left, ListNode right){ if(left == null || right == null) return null; ListNode node = new ListNode(0); ListNode dummy = node; while(left != null && right != null){ if(left.val < right.val){ ListNode tmp = left; left = left.next; dummy.next = tmp; dummy = dummy.next; }else{ ListNode tmp = right; right = right.next; dummy.next = tmp; dummy = dummy.next; } } if(left != null) dummy.next = left; else dummy.next = right; return node.next; } }

快排思路實現

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
    /**
     * @param head: The head of linked list.
     * @return: You should return the head of the sorted linked list,
                    using constant space complexity.
     */
    public ListNode sortList(ListNode head) {  
        if(head == null || head.next == null) return head;
        ListNode pivot = head;
        head = head.next;
        pivot.next = null;
        ListNode p = pivot;

        ListNode small = new ListNode(0);
        ListNode large = new ListNode(0);
        ListNode s = small;
        ListNode l = large;

        while(head != null){
            if(head.val > pivot.val){
                l.next = head;
                l = l.next;
            }else if(head.val == pivot.val){
                p.next = head;
                p = p.next;
            }else{
                s.next = head;
                s = s.next;
            }
            head = head.next;
        }
        s.next = null;
        l.next = null;
        ListNode ss = sortList(small.next);
        if(ss == null){
            ss = pivot;
        }else{
            ListNode tmp = ss;
            while(tmp.next != null){
                tmp = tmp.next;
            }
            tmp.next = pivot;
        }
        ListNode tail = sortList(large.next);
        p.next = tail;
        return ss;

    }
}