package com.fishercoder.solutions;

import com.fishercoder.common.classes.ListNode;

/**Reverse a singly linked list.*/
public class _206 {
    //creating a newHead = null is a very common/smart way to handle such cases, the logic flows out very naturally:
    //create a new node called "next" to hold current head's next node
    //then we could redirect head's next pointer to point to newHead which is head's previous node
    //the above two steps finished the reversion, to continue this process until we reach the end of the original list,
    //we'll assign current "head" to new "newHead", and current "next" to be new "head" for the next iteration, here's the code
    public ListNode reverseList_iterative(ListNode head) {
        /**It works out the best to set up a debug point and visualize this process:
         * e.g. 1->2->3-null
         * at the end of the first iteration of the while loop, the status is like this:
         * newHead: 1->null
         * head: 2->3-null
         * then it continues the iteration.*/
        ListNode newHead = null;
        while (head != null) {
            ListNode next = head.next;
            head.next = newHead;
            newHead = head;
            head = next;
        }
        return newHead;
    }

    //following the above iterative version, the recursive solution flows out so naturally, basically, we just replaced the while loop with a recursive function
    //still, a null newHead proves to be very helpful.
    public ListNode reverseList_recursive(ListNode head) {
        ListNode newHead = null;
        return reverse(head, newHead);
    }

    private ListNode reverse(ListNode head, ListNode newHead) {
        if (head != null) {
            ListNode next = head.next;
            head.next = newHead;
            newHead = head;
            head = next;
            return reverse(head, newHead);
        } else return newHead;
    }

    //the above recursive function could of course be shortened to below, but the above one is just more intuitive and easier to follow and sort out your logic
    private ListNode reverse_more_concise(ListNode head, ListNode newHead) {
        if (head != null) {
            ListNode next = head.next;
            head.next = newHead;
            return reverse_more_concise(next, head);
        } else return newHead;
    }

}
@@ -81,16 +81,51 @@
 de;%0A%0A/**
+%0A * 206. Reverse Linked List%0A *%0A * 
 Reverse 
@@ -168,22 +168,24 @@
  _206 %7B%0A
+%0A
     /
-/
+**
 creating
@@ -291,18 +291,16 @@
 ly:%0A    
-//
 create a
@@ -360,18 +360,16 @@
 ode%0A    
-//
 then we 
@@ -453,26 +453,24 @@
 us node%0A    
-//
 the above tw
@@ -575,18 +575,16 @@
 st,%0A    
-//
 we'll as
@@ -696,16 +696,18 @@
 the code
+*/
 %0A    pub
@@ -1297,17 +1297,26 @@
 %7D%0A%0A    /
-/
+**%0A     * 
 followin
@@ -1466,18 +1466,19 @@
 ion%0A    
-//
+ * 
 still, a
@@ -1518,16 +1518,24 @@
 elpful.%0A
+     */%0A
     publ
@@ -1664,32 +1664,24 @@
 %0A    %7D%0A%0A    
-private 
 ListNode rev
@@ -1733,33 +1733,33 @@
    if (head 
-!
+=
 = null) 
 %7B%0A          
@@ -1738,38 +1738,48 @@
  (head == null) 
-%7B%0A    
+return newHead;%0A
         ListNode
@@ -1797,36 +1797,32 @@
 d.next;%0A        
-    
 head.next = newH
@@ -1826,36 +1826,32 @@
 ewHead;%0A        
-  
-  
 newHead = head;%0A
@@ -1850,36 +1850,32 @@
 = head;%0A        
-    
 head = next;%0A   
@@ -1871,36 +1871,32 @@
 = next;%0A        
-   
- 
 return reverse(h
@@ -1914,467 +1914,8 @@
 d);%0A
-        %7D else return newHead;%0A    %7D%0A%0A    //the above recursive function could of course be shortened to below, but the above one is just more intuitive and easier to follow and sort out your logic%0A    private ListNode reverse_more_concise(ListNode head, ListNode newHead) %7B%0A        if (head != null) %7B%0A            ListNode next = head.next;%0A            head.next = newHead;%0A            return reverse_more_concise(next, head);%0A        %7D else return newHead;%0A
     

edit 206