归并排序---Sort List

来源:互联网 发布:json美化 编辑:程序博客网 时间:2024/05/17 06:55

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

Have you met this question in a real interview? Yes
Example
Given 1-3->2->null, sort it to 1->2->3->null.

Tags Expand

Related Problems Expand

/** * Definition for ListNode. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int val) { *         this.val = val; *         this.next = null; *     } * } */ public class Solution {                private ListNode findMiddle(ListNode head) {        ListNode slow = head, fast = head.next;        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 tail = dummy;        while (head1 != null && head2 != null) {            if (head1.val < head2.val) {                tail.next = head1;                head1 = head1.next;            } else {                tail.next = head2;                head2 = head2.next;            }            tail = tail.next;        }        if (head1 != null) {            tail.next = head1;        } else {            tail.next = head2;        }        return dummy.next;    }    public ListNode sortList(ListNode head) {        if (head == null || head.next == null) {            return head;        }        ListNode mid = findMiddle(head);        ListNode right = sortList(mid.next);        mid.next = null;        ListNode left = sortList(head);        return merge(left, right);    }}
0 0
原创粉丝点击