Advertisement
sweet1cris

Untitled

Jan 9th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.14 KB | None | 0 0
  1.  
  2. // version 1: Merge Sort
  3. public class Solution {            
  4.     private ListNode findMiddle(ListNode head) {
  5.         ListNode slow = head, fast = head.next;
  6.         while (fast != null && fast.next != null) {
  7.             fast = fast.next.next;
  8.             slow = slow.next;
  9.         }
  10.         return slow;
  11.     }    
  12.  
  13.     private ListNode merge(ListNode head1, ListNode head2) {
  14.         ListNode dummy = new ListNode(0);
  15.         ListNode tail = dummy;
  16.         while (head1 != null && head2 != null) {
  17.             if (head1.val < head2.val) {
  18.                 tail.next = head1;
  19.                 head1 = head1.next;
  20.             } else {
  21.                 tail.next = head2;
  22.                 head2 = head2.next;
  23.             }
  24.             tail = tail.next;
  25.         }
  26.         if (head1 != null) {
  27.             tail.next = head1;
  28.         } else {
  29.             tail.next = head2;
  30.         }
  31.  
  32.         return dummy.next;
  33.     }
  34.  
  35.     public ListNode sortList(ListNode head) {
  36.         if (head == null || head.next == null) {
  37.             return head;
  38.         }
  39.  
  40.         ListNode mid = findMiddle(head);
  41.  
  42.         ListNode right = sortList(mid.next);
  43.         mid.next = null;
  44.         ListNode left = sortList(head);
  45.  
  46.         return merge(left, right);
  47.     }
  48. }
  49.  
  50. // version 2: Quick Sort 1
  51. public class Solution {
  52.     public ListNode sortList(ListNode head) {
  53.         if (head == null || head.next == null) {
  54.             return head;
  55.         }
  56.        
  57.         ListNode mid = findMedian(head); // O(n)
  58.        
  59.         ListNode leftDummy = new ListNode(0), leftTail = leftDummy;
  60.         ListNode rightDummy = new ListNode(0), rightTail = rightDummy;
  61.         ListNode middleDummy = new ListNode(0), middleTail = middleDummy;
  62.         while (head != null) {
  63.             if (head.val < mid.val) {
  64.                 leftTail.next = head;
  65.                 leftTail = head;
  66.             } else if (head.val > mid.val) {
  67.                 rightTail.next = head;
  68.                 rightTail = head;
  69.             } else {
  70.                 middleTail.next = head;
  71.                 middleTail = head;
  72.             }
  73.             head = head.next;
  74.         }
  75.        
  76.         leftTail.next = null;
  77.         middleTail.next = null;
  78.         rightTail.next = null;
  79.        
  80.         ListNode left = sortList(leftDummy.next);
  81.         ListNode right = sortList(rightDummy.next);
  82.        
  83.         return concat(left, middleDummy.next, right);
  84.     }
  85.    
  86.     private ListNode findMedian(ListNode head) {
  87.         ListNode slow = head, fast = head.next;
  88.         while (fast != null && fast.next != null) {
  89.             slow = slow.next;
  90.             fast = fast.next.next;
  91.         }
  92.         return slow;
  93.     }
  94.    
  95.     private ListNode concat(ListNode left, ListNode middle, ListNode right) {
  96.         ListNode dummy = new ListNode(0), tail = dummy;
  97.        
  98.         tail.next = left; tail = getTail(tail);
  99.         tail.next = middle; tail = getTail(tail);
  100.         tail.next = right; tail = getTail(tail);
  101.         return dummy.next;
  102.     }
  103.    
  104.     private ListNode getTail(ListNode head) {
  105.         if (head == null) {
  106.            return null;
  107.         }
  108.        
  109.         while (head.next != null) {
  110.             head = head.next;
  111.         }
  112.         return head;
  113.     }
  114. }
  115.  
  116. // version 3: Quick Sort 2
  117. /**
  118.  * Definition for ListNode.
  119.  * public class ListNode {
  120.  *     int val;
  121.  *     ListNode next;
  122.  *     ListNode(int val) {
  123.  *         this.val = val;
  124.  *         this.next = null;
  125.  *     }
  126.  * }
  127.  */
  128. class Pair {
  129.     public ListNode first, second;
  130.     public Pair(ListNode first, ListNode second) {
  131.         this.first = first;
  132.         this.second = second;
  133.     }
  134. }
  135.  
  136. public class Solution {
  137.     /**
  138.      * @param head: The head of linked list.
  139.      * @return: You should return the head of the sorted linked list,
  140.                     using constant space complexity.
  141.      */
  142.     public ListNode sortList(ListNode head) {
  143.         if (head == null || head.next == null) {
  144.             return head;
  145.         }
  146.        
  147.         ListNode mid = findMedian(head); // O(n)
  148.         Pair pair = partition(head, mid.val); // O(n)
  149.        
  150.         ListNode left = sortList(pair.first);
  151.         ListNode right = sortList(pair.second);
  152.        
  153.         getTail(left).next = right; // O(n)
  154.        
  155.         return left;
  156.     }
  157.    
  158.     // 1->2->3 return 2
  159.     // 1->2 return 1
  160.     private ListNode findMedian(ListNode head) {
  161.         ListNode slow = head, fast = head.next;
  162.         while (fast != null && fast.next != null) {
  163.             slow = slow.next;
  164.             fast = fast.next.next;
  165.         }
  166.         return slow;
  167.     }
  168.    
  169.     // < value in the left, > value in the right
  170.     private Pair partition(ListNode head, int value) {
  171.         ListNode leftDummy = new ListNode(0), leftTail = leftDummy;
  172.         ListNode rightDummy = new ListNode(0), rightTail = rightDummy;
  173.         ListNode equalDummy = new ListNode(0), equalTail = equalDummy;
  174.         while (head != null) {
  175.             if (head.val < value) {
  176.                 leftTail.next = head;
  177.                 leftTail = head;
  178.             } else if (head.val > value) {
  179.                 rightTail.next = head;
  180.                 rightTail = head;
  181.             } else {
  182.                 equalTail.next = head;
  183.                 equalTail = head;
  184.             }
  185.             head = head.next;
  186.         }
  187.        
  188.         leftTail.next = null;
  189.         rightTail.next = null;
  190.         equalTail.next = null;
  191.        
  192.         if (leftDummy.next == null && rightDummy.next == null) {
  193.             ListNode mid = findMedian(equalDummy.next);
  194.             leftDummy.next = equalDummy.next;
  195.             rightDummy.next = mid.next;
  196.             mid.next = null;
  197.         } else if (leftDummy.next == null) {
  198.             leftTail.next = equalDummy.next;
  199.         } else {
  200.             rightTail.next = equalDummy.next;
  201.         }
  202.        
  203.         return new Pair(leftDummy.next, rightDummy.next);
  204.     }
  205.    
  206.     private ListNode getTail(ListNode head) {
  207.         if (head == null) {
  208.            return null;
  209.         }
  210.        
  211.         while (head.next != null) {
  212.             head = head.next;
  213.         }
  214.         return head;
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement