-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0092ReverseLinkedListii.java
More file actions
82 lines (65 loc) · 1.92 KB
/
_0092ReverseLinkedListii.java
File metadata and controls
82 lines (65 loc) · 1.92 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.heatwave.leetcode.problems;
/**
* 92. Reverse Linked List II
* Reverse a linked list from position m to n. Do it in one-pass.
* <p>
* Note: 1 ≤ m ≤ n ≤ length of list.
* <p>
* Example:
* <p>
* Input: 1->2->3->4->5->NULL, m = 2, n = 4
* Output: 1->4->3->2->5->NULL
*/
public class _0092ReverseLinkedListii {
public static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
static class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (m == n)
return head;
ListNode prev = null, next, p = head;
ListNode left = null, right = null;
int count = 1;
while (p != null && count <= n) {
next = p.next;
if (count == m) {
left = prev;
right = p;
}
if (count >= m)
p.next = prev;
if (count == n) {
if (left != null)
left.next = p;
assert right != null;
right.next = next;
if (m == 1)
head = p;
}
prev = p;
p = next;
count++;
}
return head;
}
}
public static void main(String[] args) {
ListNode listNode = new ListNode(1);
listNode.next = new ListNode(2);
listNode.next.next = new ListNode(3);
listNode.next.next.next = new ListNode(4);
listNode.next.next.next.next = new ListNode(5);
Solution solution = new Solution();
listNode = solution.reverseBetween(listNode, 2, 4);
ListNode p = listNode;
while (p != null) {
System.out.println(p.val);
p = p.next;
}
}
}