Sort a linked list in O(n log n) time using constant space complexity.
/**
* 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;
ListNode 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 pointer = dummy;
while (head1 != null && head2 != null) {
if (head1.val < head2.val) {
pointer.next = head1;
head1 = head1.next;
} else {
pointer.next = head2;
head2 = head2.next;
}
pointer = pointer.next;
}
if (head1 != null) {
pointer.next = head1;
} else {
pointer.next = head2;
}
return dummy.next;
}
/**
* @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 = findMiddle(head);
ListNode right = sortList(mid.next);
mid.next = null;
ListNode left = sortList(head);
return merge(left, right);
}
}