The problem:
Sort a linked list in O(n log n) time using constant space complexity.
My analysis:
The idea behind this problem is easy : merge sort !
But we should learn some tricky skills from this question.
1. How to split a linked list into two separate linked lists?
a. use two pointers: walker and runner.
a.1 walker move one step in each iteration.
a.2 runner move two steps in each iteration.
b. iterate until runner can‘t move any more (runner.next == null || runner.next.next == null)
c. then head1 is the head of original list, and head2 is the next element of runner.
2. How to merge two linked list in an elegant way?
The key idea behind writing an elegant code is always use the same invarint.
To achieve this purpose, I use a dummy head as fake head and a pre pinter always point to the last element of the resulting list.
ListNode dummy = new ListNode(0);
pre = dummy;
...
while () {
pre.next = ...;
}
return dummy.next;
My code:
/** * 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) { return mergeSort(head); } private ListNode mergeSort(ListNode head) { if (head == null || head.next == null) //the base case in the recursion is very important! return head; ListNode walker = head; ListNode runner = head; while (runner.next != null && runner.next.next != null) { //skill: check runner.next at first. walker = walker.next; //this skill is amazing!!! runner = runner.next.next; } ListNode head2 = walker.next; walker.next = null; ListNode head1 = head; head1 = mergeSort(head1); head2 = mergeSort(head2); head = merge(head1, head2); return head; } private ListNode merge(ListNode head1, ListNode head2) { if (head1 == null && head2 == null) return null; if (head1 == null && head2 != null) return head2; if (head1 != null && head2 == null) return head1; ListNode dummy = new ListNode(0); ListNode pre = dummy; ListNode ptr1 = head1; ListNode ptr2 = head2; while (ptr1 != null && ptr2 != null) { if (ptr1.val <= ptr2.val) { pre.next = ptr1; pre = pre.next; ptr1 = ptr1.next; } else { pre.next = ptr2; pre = pre.next; ptr2 = ptr2.next; } } if (ptr1 == null) pre.next = ptr2; else pre.next = ptr1; return dummy.next; } }