quick sort version 1:
quick sort 定义: https://en.wikipedia.org/wiki/Quicksort
quick sort 核心部分为partition(http://www.cnblogs.com/jiangchen/p/5398166.html),
取list中间点(http://www.cnblogs.com/jiangchen/p/5529704.html)
时间复杂度为o(nlgn)
1 public class Solution { 2 //recursive partition list to left,middle,right 3 parts, use concat function to conntet 3 parts together at end of function 3 public ListNode sortList(ListNode head) { 4 if (head == null || head.next == null) { 5 return head; 6 } 7 ListNode middle = findMedian(head); 8 9 ListNode leftDummy = new ListNode(0), leftTail = leftDummy; 10 ListNode rightDummy = new ListNode(0), rightTail = rightDummy; 11 ListNode middleDummy = new ListNode(0), middleTail = middleDummy; 12 13 while (head != null) { 14 if (head.val < middle.val) { 15 leftTail.next = head; 16 leftTail = head; 17 } else if(head.val == middle.val) { 18 middleTail.next = head; 19 middleTail = head; 20 } else { 21 rightTail.next = head; 22 rightTail = head; 23 } 24 head = head.next; 25 } 26 leftTail.next = null; 27 middleTail.next = null; 28 rightTail.next = null; 29 30 ListNode left = sortList(leftDummy.next); 31 ListNode right = sortList(rightDummy.next); 32 33 return concat(left, middleDummy.next, right); 34 } 35 // connect 3 part together 36 private ListNode concat (ListNode left, ListNode mid, ListNode right) { 37 ListNode dummy = new ListNode(0), tail = dummy; 38 tail.next = left; 39 tail = getTail(tail);//left 本身可能是一个空串,所以3次getTail都用tail作为起始点,而不用left,mid,right 40 tail.next = mid; 41 tail = getTail(tail); 42 tail.next = right; 43 tail = getTail(tail); 44 return dummy.next; 45 } 46 //get end of current list 47 private ListNode getTail (ListNode current) { 48 if (current == null) { 49 return current; 50 } 51 while (current.next != null) { 52 current = current.next; 53 } 54 return current; 55 } 56 57 private ListNode findMedian(ListNode head) { 58 ListNode slow = head, fast = head.next; 59 while (fast != null && fast.next != null) { 60 slow = slow.next; 61 fast = fast.next.next; 62 } 63 return slow; 64 } 65 }
merge sort version 1:
merge sort 定义: https://en.wikipedia.org/wiki/Merge_sort
merge sort 核心部分为 recusive devide linked list to two part
merge a list
1 public class Solution { 2 private ListNode findMiddle(ListNode head) { 3 ListNode slow = head, fast = head.next; 4 while (fast != null && fast.next != null) { 5 fast = fast.next.next; 6 slow = slow.next; 7 } 8 return slow; 9 } 10 11 private ListNode merge(ListNode head1, ListNode head2) { 12 ListNode tail,dummy = new ListNode(0); 13 tail = dummy; 14 while (head1 != null || head2 != null) { 15 if (head2 != null && (head1 == null || head1.val > head2.val)) { 16 tail.next = head2; 17 tail = head2; 18 head2 = head2.next; 19 } else { 20 tail.next = head1; 21 tail = head1; 22 head1 = head1.next; 23 } 24 } 25 return dummy.next; 26 } 27 28 public ListNode sortList(ListNode head) { 29 ListNode dummyHead1, dummyHead2; 30 if (head == null || head.next == null) { 31 return head; 32 } 33 ListNode head1, head2, mid; 34 mid = findMiddle(head); 35 head1 = head; 36 head2 = mid.next; 37 mid.next = null; 38 // System.out.println(head1.val); 39 //System.out.println(head2.val); 40 41 head1 = sortList(head1); 42 head2 = sortList(head2); 43 44 return merge(head1,head2); 45 } 46 }
1 // version 1: Merge Sort 2 public class Solution { 3 private ListNode findMiddle(ListNode head) { 4 ListNode slow = head, fast = head.next; 5 while (fast != null && fast.next != null) { 6 fast = fast.next.next; 7 slow = slow.next; 8 } 9 return slow; 10 } 11 12 private ListNode merge(ListNode head1, ListNode head2) { 13 ListNode dummy = new ListNode(0); 14 ListNode tail = dummy; 15 while (head1 != null && head2 != null) { 16 if (head1.val < head2.val) { 17 tail.next = head1; 18 head1 = head1.next; 19 } else { 20 tail.next = head2; 21 head2 = head2.next; 22 } 23 tail = tail.next; 24 } 25 if (head1 != null) { 26 tail.next = head1; 27 } else { 28 tail.next = head2; 29 } 30 31 return dummy.next; 32 } 33 34 public ListNode sortList(ListNode head) { 35 if (head == null || head.next == null) { 36 return head; 37 } 38 39 ListNode mid = findMiddle(head); 40 41 ListNode right = sortList(mid.next); 42 mid.next = null; 43 ListNode left = sortList(head); 44 45 return merge(left, right); 46 } 47 }