# Question

Formatted question description: https://leetcode.ca/all/92.html

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.



# Algorithm

Take the example in the title, the three points that are transformed are 2, 3, and 4. We need to find the first node before the transformation node, as long as the pre is moved backward by m-1 steps, Why do you want to subtract 1, because the problem is counting from 1, here only one step is taken, which is node 1, and use pre to point to it. What if the node 1 starts to change? This is why we use the dummy node. Pre can also point to the dummy node. Then the exchange is about to begin. Since only two nodes can be exchanged at a time, we follow the exchange sequence as follows:

1 -> 2 -> 3 -> 4 -> 5 -> NULL

1 -> 3 -> 2 -> 4 -> 5 -> NULL

1 -> 4 -> 3 -> 2 -> 5 -> NULL


This problem is a follow-up problem for problem 206. In this problem, only the nodes from position m to n should be reversed, and the reverse operation should be done in one-pass. The iterative solution for problem 206 can be adapted here, with some modification.

Obviously, if the list is null, or there is only one element in the list, or position m is the same as position n, then no reverse needs to be done and just return the original list.

Since the head may also be reversed, create a dummyHead whose next pointer points to head. Find the reverse start node, the reverse end node, the last node before reverse and the first node after reverse, and then do the reverse operation.

For example, for linked list 1->2->3->4->5->NULL, m = 2, n = 4, the reverse start node is 2, the reverse end node is 4, the last node before reverse is 1, and the first node after reverse is 5.

Like the iterative solution for problem 206, use two pointers prev and curr to do the iteration. After the loop, set the last node before reverse to point to the reverse end node (which is the start of the reverse part after reverse), and set the reverse start node (which is the end of the reverse part after reverse) to point to the first node after reverse.

Finally, return dummyHead.next.

Java