Sort List (Frequent++)

来源:互联网 发布:linux find mtime时间 编辑:程序博客网 时间:2024/06/07 22:15

http://www.lintcode.com/en/problem/sort-list/

题目:将链表排序,使得时间复杂度为O(nlogn)

解答:

方法一:归并排序

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) {  
        // write your code here
        if (head == null || head.next == null) {
            return head;
        }
        ListNode mid = middle(head);
        ListNode right = sortList(mid.next);
        mid.next = null;
        ListNode left = sortList(head);
        return merge(left, right);
    }
    private ListNode middle(ListNode head) {
        ListNode fast = head.next;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    private ListNode merge(ListNode head1, ListNode head2) {
        ListNode dummy = new ListNode(0);
        ListNode head = dummy;
        while (head1 != null && head2 != null) {
            if (head1.val < head2.val) {
                head.next = head1;
                head = head.next;
                head1 = head1.next;
            } else {
                head.next = head2;
                head = head.next;
                head2 = head.next;
            }
        }
        while (head1 != null) {
            head.next = head1;
            head = head.next;
            head1 = head1.next;
        }
        while (head2 != null) {
            head.next = head2;
            head = head.next;
            head2 = head.next;
        }
        return dummy.next;
     }
}


方法二:快速排序

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) {  
        // write your code here
        if (head == null || head.next == null) {
            return head;
        }
        
        ListNode leftDummy = new ListNode(0), leftHead = leftDummy;
        ListNode rightDummy = new ListNode(0), rightHead = rightDummy;
        ListNode middleDummy = new ListNode(0), middleHead = middleDummy;
        ListNode mid = middle(head);
        
        while (head != null) {
            if (head.val > mid.val) {
                rightHead.next = head;
                rightHead = rightHead.next;
            } else if (head.val < mid.val) {
                leftHead.next = head;
                leftHead = leftHead.next;
            } else {
                middleHead.next = head;
                middleHead = middleHead.next;
            }
            head = head.next;
        }
        
        rightHead.next = null;
        leftHead.next = null;
        middleHead.next = null;
        
        ListNode left = sortList(leftDummy.next);
        ListNode right = sortList(rightDummy.next);
        return merge(left, middleDummy.next, right);
    }
    private ListNode middle(ListNode head) {
        ListNode fast = head.next;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
    private ListNode merge(ListNode head1, ListNode head2, ListNode head3) {
        ListNode tail1 = head1;
        ListNode tail2 = head2;
        while (tail2.next != null) {
            tail2 = tail2.next;
        }
        if (head1 != null){
            while (tail1.next != null) {
              tail1 = tail1.next;
            }
            tail1.next = head2;
            tail2.next = head3;
            return head1;
        } else {
            tail2.next = head3;
            return head2;
        }
    }
}

原创粉丝点击