148. Sort List
Sort a linked list in O(n log n) time using constant space complexity.
Example
Given 1->3->2->null
, sort it to 1->2->3->null
.
Merge Sort version
1 /** 2 * Definition for ListNode. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int val) { 7 * this.val = val; 8 * this.next = null; 9 * } 10 * } 11 */ 12 public class Solution { 13 /** 14 * @param head: The head of linked list. 15 * @return: You should return the head of the sorted linked list, 16 using constant space complexity. 17 */ 18 public ListNode sortList(ListNode head) { 19 return mergeSort(head); 20 } 21 22 private ListNode mergeSort(ListNode head) { 23 if (head == null || head.next == null) return head; 24 ListNode walker = head; 25 ListNode runner = head; 26 while (runner.next != null && runner.next.next != null) { 27 walker = walker.next; 28 runner = runner.next.next; 29 } 30 ListNode head2 = walker.next; 31 walker.next = null; 32 ListNode head1 = head; 33 head1 = mergeSort(head1); 34 head2 = mergeSort(head2); 35 return merge(head1, head2); 36 } 37 private ListNode merge(ListNode head1, ListNode head2) 38 { 39 ListNode fakeHead = new ListNode(0); 40 ListNode current = fakeHead; 41 while (head1 != null && head2 != null) { 42 if (head1.val <= head2.val) { 43 current.next = head1; 44 head1 = head1.next; 45 } else { 46 current.next = head2; 47 head2 = head2.next; 48 } 49 current = current.next; 50 } 51 52 if (head1 != null) { 53 current.next = head1; 54 } 55 56 if (head2 != null) { 57 current.next = head2; 58 } 59 return fakeHead.next; 60 } 61 }
Quick Sort:
这里说一下,一定要有三个list, 如果只有两个,会出现死循环。
1 public class Solution { 2 public ListNode sortList(ListNode head) { 3 if (head == null || head.next == null) return head; 4 int pivot = head.val; 5 6 ListNode leftDummy = new ListNode(0); 7 ListNode leftHead = leftDummy; 8 9 ListNode rightDummy = new ListNode(0); 10 ListNode rightHead = rightDummy; 11 12 ListNode middleDummy = new ListNode(0); 13 ListNode middleHead = middleDummy; 14 15 while (head != null) { 16 if (head.val < pivot) { 17 leftHead.next = head; 18 leftHead = head; 19 } else if (head.val > pivot) { 20 rightHead.next = head; 21 rightHead = head; 22 } else { 23 middleHead.next = head; 24 middleHead = head; 25 } 26 head = head.next; 27 } 28 29 leftHead.next = null; 30 middleHead.next = null; 31 rightHead.next = null; 32 33 ListNode left = sortList(leftDummy.next); 34 ListNode right = sortList(rightDummy.next); 35 36 return concat(left, middleDummy.next, right); 37 } 38 39 40 private ListNode concat(ListNode left, ListNode middle, ListNode right) { 41 ListNode dummy = new ListNode(0); 42 ListNode tail = dummy; 43 tail.next = left; 44 tail = getTail(tail); 45 tail.next = middle; 46 tail = getTail(tail); 47 tail.next = right;
49 return dummy.next; 50 } 51 52 53 private ListNode getTail(ListNode head) { 54 if (head == null) { 55 return null; 56 } 57 58 while (head.next != null) { 59 head = head.next; 60 } 61 return head; 62 } 63 }
转载请注明出处:cnblogs.com/beiyeqingteng/