148. Sort List
题目:Sort a linked list in O(n log n) time using constant space complexity.
题意:排序链表
思路I:merge sort
复杂度分析:时间复杂度O(nlgn),空间复杂度O(1)
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 10 // merge sort version 11 class Solution { 12 public ListNode sortList(ListNode head) { 13 if (head == null || head.next == null) { 14 return head; 15 } 16 ListNode middle = getMiddle(head); 17 ListNode rightHead = middle.next; 18 middle.next = null; 19 ListNode left = sortList(head); 20 ListNode right = sortList(rightHead); 21 return merge(left, right); 22 } 23 public ListNode getMiddle(ListNode head) { 24 if (head == null || head.next == null) { 25 return head; 26 } 27 ListNode slow = head; 28 ListNode fast = head.next; 29 while (fast != null && fast.next != null) { 30 slow = slow.next; 31 fast = fast.next.next; 32 } 33 return slow; 34 } 35 public ListNode merge(ListNode left, ListNode right) { 36 ListNode dummy = new ListNode(0); 37 ListNode cur = dummy; 38 while (left != null && right != null) { 39 if (left.val <= right.val) { 40 cur.next = left; 41 left = left.next; 42 } else { 43 cur.next = right; 44 right = right.next; 45 } 46 cur = cur.next; 47 } 48 if (left != null) { 49 cur.next = left; 50 } 51 if (right != null) { 52 cur.next = right; 53 } 54 return dummy.next; 55 } 56 }
思路II:quick sort
复杂度分析:时间复杂度O(nlgn),空间复杂度O(1)
1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { val = x; } 7 * } 8 */ 9 10 // quick sort version 11 class Solution { 12 public ListNode sortList(ListNode head) { 13 if (head == null || head.next == null) { 14 return head; 15 } 16 ListNode middle = getMiddle(head); 17 ListNode leftDummy = new ListNode(0), leftTail = leftDummy; 18 ListNode middleDummy = new ListNode(0), middleTail = middleDummy; 19 ListNode rightDummy = new ListNode(0), rightTail = rightDummy; 20 while (head != null) { 21 if ( head.val < middle.val) { 22 leftTail.next = head; 23 leftTail = leftTail.next; 24 } else if (head.val == middle.val) { 25 middleTail.next = head; 26 middleTail = middleTail.next; 27 } else { 28 rightTail.next = head; 29 rightTail = rightTail.next; 30 } 31 head = head.next; 32 } 33 leftTail.next = null; middleTail.next = null; rightTail.next = null; 34 ListNode left = sortList(leftDummy.next); 35 ListNode right = sortList(rightDummy.next); 36 return merge(merge(left, middleDummy.next), right); 37 } 38 public ListNode getMiddle(ListNode head) { 39 if (head == null || head.next == null) { 40 return head; 41 } 42 ListNode slow = head; 43 ListNode fast = head.next; 44 while (fast != null && fast.next != null) { 45 slow = slow.next; 46 fast = fast.next.next; 47 } 48 return slow; 49 } 50 public ListNode merge(ListNode left, ListNode right) { 51 ListNode dummy = new ListNode(0); 52 ListNode cur = dummy; 53 while (left != null && right != null) { 54 if (left.val <= right.val) { 55 cur.next = left; 56 left = left.next; 57 } else { 58 cur.next = right; 59 right = right.next; 60 } 61 cur = cur.next; 62 } 63 if (left != null) { 64 cur.next = left; 65 } 66 if (right != null) { 67 cur.next = right; 68 } 69 return dummy.next; 70 } 71 }