package com.fishercoder.solutions;

import com.fishercoder.common.classes.ListNode;

/**
 * 92. Reverse Linked List II
 *
 * Reverse a linked list from position m to n. Do it in-place and in one-pass.
 *
 * For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4,
 *
 * return 1->4->3->2->5->NULL.
 *
 * Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list.
 */
public class _92 {

  public static class Solution1 {
    /** credit: https://discuss.leetcode.com/topic/8976/simple-java-solution-with-clear-explanation */
    public ListNode reverseBetween(ListNode head, int m, int n) {
      // use four nodes, pre, start, then, dummy
      // just reverse the nodes along the way
      ListNode dummy = new ListNode(-1);
      dummy.next = head;
      ListNode pre = dummy;
      for (int i = 0; i < m - 1; i++) {
        pre = pre.next;
      }

      ListNode start = pre.next;// start is the node prior to reversing, in the given example,
      // start is node with value 1
      ListNode then = start.next;// then is the node that we'll start to reverse, in the given
      // example, it's 2

      for (int i = 0; i < n - m; i++) {
        // pay special attention to this for loop, it's assigning then.next to start.next, it
        // didn't initialize a new node
        // this does exactly what I desired to do, but I just didn't figure out how to implement
        // it, thumbs up to the OP!
        start.next = then.next;
        then.next = pre.next;
        pre.next = then;
        then = start.next;
      }

      return dummy.next;
    }
  }
}

@@ -82,309 +82,8 @@
 e;%0A%0A
-/**%0A * 92. Reverse Linked List II%0A *%0A * Reverse a linked list from position m to n. Do it in-place and in one-pass.%0A *%0A * For example: Given 1-%3E2-%3E3-%3E4-%3E5-%3ENULL, m = 2 and n = 4,%0A *%0A * return 1-%3E4-%3E3-%3E2-%3E5-%3ENULL.%0A *%0A * Note: Given m, n satisfy the following condition: 1 %E2%89%A4 m %E2%89%A4 n %E2%89%A4 length of list.%0A */%0A
 publ
@@ -98,16 +98,18 @@
  _92 %7B%0A%0A
+  
   public
@@ -142,10 +142,25 @@
     
-/*
+    /**%0A         
 * cr
@@ -252,12 +252,25 @@
 tion
- */%0A
+%0A         */%0A    
     
@@ -327,24 +327,30 @@
 m, int n) %7B%0A
+      
       // use
@@ -392,16 +392,22 @@
 y%0A      
+   
+   
 // just 
@@ -440,24 +440,30 @@
 e way%0A      
+     
+ 
 ListNode dum
@@ -485,16 +485,22 @@
 de(-1);%0A
+      
       du
@@ -516,16 +516,22 @@
 = head;%0A
+      
       Li
@@ -550,16 +550,22 @@
  dummy;%0A
+      
       fo
@@ -604,16 +604,24 @@
         
+   
+     
 pre = pr
@@ -634,25 +634,37 @@
 ;%0A      
-%7D%0A%0A
+      %7D%0A%0A     
       
+ 
 ListNode
@@ -744,16 +744,22 @@
 xample,%0A
+      
       //
@@ -788,24 +788,30 @@
 lue 1%0A      
+ 
+     
 ListNode the
@@ -893,16 +893,22 @@
 n%0A      
+  
+    
 // examp
@@ -919,16 +919,22 @@
 it's 2%0A%0A
+      
       fo
@@ -973,16 +973,24 @@
         
+      
+  
 // pay s
@@ -1067,16 +1067,24 @@
 ext, it%0A
+        
         
@@ -1123,16 +1123,24 @@
         
+     
+   
 // this 
@@ -1220,16 +1220,24 @@
 plement%0A
+        
         
@@ -1272,16 +1272,24 @@
         
+     
+   
 start.ne
@@ -1308,24 +1308,32 @@
 xt;%0A        
+     
+   
 then.next = 
@@ -1350,16 +1350,24 @@
         
+    
+    
 pre.next
@@ -1379,24 +1379,32 @@
 en;%0A        
+    
+    
 then = start
@@ -1412,27 +1412,39 @@
 next;%0A      
+   
-%7D%0A%0A
+   %7D%0A%0A      
       return
@@ -1460,16 +1460,22 @@
 xt;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 92