Description
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example
1 | Input: 1->2->3->4->5->NULL, m = 2, n = 4 |
Solution
It basically does two things:
- start, which records mNode, bubbles up step by step to where the nNode originally is; start node never change, it is always the mNode;
- next, which is always the next node of start (obviously it changes every time), is repeatedly being put to the next position of the pre node ( pre node doesn’t change either), so that nodes between pre (not include) and start (include) is always in descending order.
1 | /** |