-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathReverseList2.java
More file actions
54 lines (47 loc) · 1.19 KB
/
ReverseList2.java
File metadata and controls
54 lines (47 loc) · 1.19 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package algorithm.lc;
/**
* 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.
*
*
*/
public class ReverseList2 {
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public static class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
// Start typing your Java solution below
// DO NOT write main() function
int len = n - m;
ListNode fakeHead = new ListNode(0);
fakeHead.next = head;
ListNode prev = fakeHead;
for (int i = 0; i < m - 1; ++i) {
prev = prev.next;
}
ListNode before = prev;
ListNode reverseHead = prev.next;
ListNode cur = reverseHead;
int count = 0;
while (count++ <= len) {
ListNode next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
reverseHead.next = cur;
before.next = prev;
return fakeHead.next;
}
}
}