@@ -0,0 +1,1854 @@
+/***%0A * Reverse a linked list from position m to n. Do it in-place and in one-pass.%0A *%0A * For example:%0A * Given 1-%3E2-%3E3-%3E4-%3E5-%3ENULL, m = 2 and n = 4,%0A *%0A * return 1-%3E4-%3E3-%3E2-%3E5-%3ENULL.%0A *%0A * Note:%0A * Given m, n satisfy the following condition:%0A * 1 %3C= m %3C= n %3C= length of list.%0A */%0A/**%0A * Definition for singly-linked list.%0A * public class ListNode %7B%0A *     int val;%0A *     ListNode next;%0A *     ListNode(int x) %7B%0A *         val = x;%0A *         next = null;%0A *     %7D%0A * %7D%0A */%0Apublic class ReverseLinkedListII %7B%0A    // Solution: O(n) O(1)%0A    public ListNode reverseBetween(ListNode head, int m, int n) %7B%0A        if (m == n)%0A            return head;%0A        ListNode ret = new ListNode(0);%0A        ret.next = head;%0A        ListNode prev = ret;%0A        int kk = m;%0A        while (--kk %3E 0)%0A            prev = prev.next;%0A        ListNode head2 = prev;%0A        System.out.printf(%22head2 val: %25d%5Cn%22, head2.val);%0A%0A        prev = prev.next;%0A        ListNode cur = prev.next;%0A        for (int i = m; i %3C n; ++i) %7B%0A            // do as follows%0A            // 0: 1 - 2 - 3. - 4%0A            // 1: 1 - (3) - 2 - 4.%0A            // 2: 1 - (4) - 3 - 2%0A            System.out.printf(%22i: %25d, cur: %25d%5Ct%22, i, cur.val);%0A            prev.next = cur.next;%0A            cur.next = head2.next;%0A            head2.next = cur;%0A            cur = prev.next;%0A        %7D%0A        System.out.println();%0A        return ret.next;%0A    %7D%0A    public static void main (String args%5B%5D ) %7B%0A        ReverseLinkedListII rll2 = new ReverseLinkedListII();%0A        ListNode n1 = new ListNode(1);%0A        ListNode n2 = new ListNode(2);%0A        ListNode n3 = new ListNode(3);%0A        n1.next = n2;%0A        n2.next = n3;%0A        ListNode head = rll2.reverseBetween(n1, 1, 2);%0A        while (head != null) %7B%0A            System.out.printf(%22val %25d%5Cn%22, head.val);%0A            head = head.next;%0A        %7D%0A    %7D%0A%7D%0A

add ReverseLinkedListII.java