0%

Leetcode092-reverseLinkedListII

Description

Reverse a linked list from position m to n. Do it in one-pass.

Note: 1 ≤ m ≤ n ≤ length of list.

Example

1
2
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL

Solution

It basically does two things:

  1. start, which records mNode, bubbles up step by step to where the nNode originally is; start node never change, it is always the mNode;
  2. 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null) return null;

ListNode dummy = new ListNode(0);
dummy.next = head;

ListNode pre = dummy;
for (int i = 0; i < m - 1; i++) pre = pre.next;
ListNode start = pre.next;
ListNode next = start.next;

for (int i = 0; i < n - m; i++){
start.next = next.next;
next.next = pre.next;
pre.next = next;
next = start.next;
}

return dummy.next;
}
}