package com.fishercoder.solutions;

import com.fishercoder.common.classes.ListNode;

/**
 * 206. Reverse Linked List
 *
 * Reverse a singly linked list.
 *
 * Example:
 * Input: 1->2->3->4->5->NULL
 * Output: 5->4->3->2->1->NULL
 *
 * Follow up:
 * A linked list can be reversed either iteratively or recursively. Could you implement both?
 * */
public class _206 {

    public static class Solution1 {
        public ListNode reverseList(ListNode head) {
            ListNode prev = null;
            ListNode curr = head;
            while (curr != null) {
                ListNode next = curr.next;
                curr.next = prev;
                prev = curr;
                curr = next;
            }
            return prev;
        }
    }

    public static class Solution2 {
        /**
         * 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(ListNode head) {
            return reverse(head, null);
        }

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

}
@@ -783,269 +783,8 @@
 2 %7B%0A
-        /**%0A         * following the above iterative version, the recursive solution flows out so naturally:%0A         * basically, we just replaced the while loop with a recursive function%0A         * still, a null newHead proves to be very helpful.%0A         */%0A
     

refactor 206