package linkedlist;

/**
 * https://oj.leetcode.com/problems/reverse-linked-list-ii/
 * Reverse a linked list from position m to n. Do it in-place and in one-pass.
 * <p/>
 * For example:
 * Given 1->2->3->4->5->NULL, m = 2 and n = 4,
 * <p/>
 * return 1->4->3->2->5->NULL.
 * <p/>
 * Note:
 * Given m, n satisfy the following condition:
 * 1 ≤ m ≤ n ≤ length of list.
 */
public class ReverseLinkedListII {
    /**
     * Definition for singly-linked list.
     */
    private static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public ListNode reverseBetween(ListNode head, int m, int n) {
        return null;
    }
}

@@ -405,16 +405,826 @@
 I %7B%0A
+    public ListNode reverseBetween(ListNode head, int m, int n) %7B%0A        if (head == null %7C%7C m %3E n %7C%7C n %3C 0 %7C%7C m == n) return head;%0A        int index = 1;%0A        ListNode dummy = new ListNode(0);%0A        dummy.next = head;%0A        ListNode node = dummy;%0A        while (node != null && index %3C m) %7B%0A            index++;%0A            node = node.next;%0A        %7D%0A        node.next = reverse(node.next, n - m);%0A        return dummy.next;%0A    %7D%0A%0A    private ListNode reverse(ListNode head, int n) %7B%0A        ListNode prev = null, node = head;%0A        int index = 0;%0A        while (node != null && index %3C= n) %7B%0A            ListNode next = node.next;%0A            node.next = prev;%0A            prev = node;%0A            node = next;%0A            index++;%0A        %7D%0A        head.next = node;%0A        return prev;%0A    %7D%0A%0A
     /**%0A
     
@@ -1219,16 +1219,16 @@
     /**%0A
-
      * D
@@ -1414,24 +1414,24 @@
 ext = null;%0A
+
         %7D%0A  
@@ -1437,101 +1437,7 @@
    %7D
-%0A%0A    public ListNode reverseBetween(ListNode head, int m, int n) %7B%0A        return null;%0A    %7D
 %0A%7D%0A

implement reverse linked list ii