package com.fishercoder.solutions;

import com.fishercoder.common.classes.ListNode;

/**
 * 148. Sort List
 *
 * Sort a linked list in O(n log n) time using constant space complexity.
 */
public class _148 {

    /**Credit: https://discuss.leetcode.com/topic/18100/java-merge-sort-solution
     * But this is not using constant space.*/
    public ListNode sortList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        //Step 1: split the list into halves
        ListNode prev = null;
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            prev = slow;
            fast = fast.next.next;
            slow = slow.next;
        }
        prev.next = null;

        //step 2: sort each half
        ListNode l1 = sortList(head);
        ListNode l2 = sortList(slow);

        //step 3: merge the two halves
        return merge(l1, l2);
    }

    private ListNode merge(ListNode l1, ListNode l2) {
        ListNode result = new ListNode(0);
        ListNode tmp = result;

        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                tmp.next = l1;
                l1 = l1.next;
            } else {
                tmp.next = l2;
                l2 = l2.next;
            }
            tmp = tmp.next;
        }

        if (l1 != null) {
            tmp.next = l1;
        }
        if (l2 != null) {
            tmp.next = l2;
        }
        return result.next;
    }

}

@@ -206,19 +206,71 @@
  %7B%0A%0A    
-/**
+public static class Solution1 %7B%0A        /**%0A         * 
 Credit: 
@@ -340,16 +340,20 @@
 on%0A     
+    
 * But th
@@ -383,19 +383,33 @@
 t space.
-*/%0A
+%0A         */%0A    
     publ
@@ -442,24 +442,28 @@
 ode head) %7B%0A
+    
         if (
@@ -507,24 +507,28 @@
             
+    
 return head;
@@ -528,35 +528,43 @@
 n head;%0A        
-%7D%0A%0A
+    %7D%0A%0A    
         //Step 1
@@ -592,32 +592,36 @@
  halves%0A        
+   
+ 
 ListNode prev = 
@@ -626,16 +626,20 @@
 = null;%0A
+    
         
@@ -660,32 +660,36 @@
 = head;%0A        
+   
+ 
 ListNode fast = 
@@ -686,32 +686,36 @@
 de fast = head;%0A
+    
         while (f
@@ -758,24 +758,28 @@
             
+  
+  
 prev = slow;
@@ -779,16 +779,20 @@
 = slow;%0A
+    
         
@@ -830,16 +830,20 @@
         
+    
 slow = s
@@ -848,24 +848,28 @@
  slow.next;%0A
+    
         %7D%0A  
@@ -870,24 +870,28 @@
   %7D%0A        
+    
 prev.next = 
@@ -897,16 +897,20 @@
  null;%0A%0A
+    
         
@@ -934,32 +934,36 @@
 ch half%0A        
+    
 ListNode l1 = so
@@ -976,32 +976,36 @@
 (head);%0A        
+   
+ 
 ListNode l2 = so
@@ -1019,16 +1019,20 @@
 slow);%0A%0A
+    
         
@@ -1066,24 +1066,28 @@
 ves%0A        
+   
+ 
 return merge
@@ -1096,16 +1096,20 @@
 1, l2);%0A
+    
     %7D%0A%0A 
@@ -1111,16 +1111,20 @@
  %7D%0A%0A    
+  
+  
 private 
@@ -1166,32 +1166,36 @@
 e l2) %7B%0A        
+    
 ListNode result 
@@ -1213,32 +1213,36 @@
 ode(0);%0A        
+  
+  
 ListNode tmp = r
@@ -1249,16 +1249,20 @@
 esult;%0A%0A
+    
         
@@ -1304,24 +1304,28 @@
             
+ 
+   
 if (l1.val %3C
@@ -1331,24 +1331,28 @@
 %3C l2.val) %7B%0A
+    
             
@@ -1386,16 +1386,20 @@
         
+    
 l1 = l1.
@@ -1416,16 +1416,20 @@
         
+   
+ 
 %7D else %7B
@@ -1429,16 +1429,20 @@
  else %7B%0A
+    
         
@@ -1480,16 +1480,20 @@
         
+   
+ 
 l2 = l2.
@@ -1502,34 +1502,40 @@
 xt;%0A            
-%7D%0A
+    %7D%0A  
             tmp 
@@ -1530,16 +1530,18 @@
         
+  
 tmp = tm
@@ -1548,32 +1548,36 @@
 p.next;%0A        
+    
 %7D%0A%0A        if (l
@@ -1567,24 +1567,28 @@
  %7D%0A%0A        
+ 
+   
 if (l1 != nu
@@ -1585,32 +1585,36 @@
  (l1 != null) %7B%0A
+    
             tmp.
@@ -1624,34 +1624,42 @@
 t = l1;%0A        
-%7D%0A
+    %7D%0A    
         if (l2 !
@@ -1672,32 +1672,36 @@
 ) %7B%0A            
+    
 tmp.next = l2;%0A 
@@ -1699,34 +1699,42 @@
 t = l2;%0A        
+   
+ 
 %7D%0A
+    
         return r
@@ -1749,13 +1749,22 @@
 xt;%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D%0A

refactor 148