Skip to content

Commit 8a57257

Browse files
Sean PrashadSean Prashad
authored andcommitted
Update 92_Reverse_Linked_List_II.java
1 parent 23bfa4a commit 8a57257

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

In-place Reveral of a Linked List/92_Reverse_Linked_List_II.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
class Solution {
22
public ListNode reverseBetween(ListNode head, int m, int n) {
3+
if (head == null) {
4+
return null;
5+
}
6+
37
ListNode dummy = new ListNode(-1);
48
dummy.next = head;
59

6-
ListNode begin = dummy;
10+
ListNode prev = dummy;
711

812
for (int i = 0; i < m - 1; i++) {
9-
begin = begin.next;
13+
prev = prev.next;
1014
}
1115

12-
ListNode start = begin.next, then = start.next;
16+
ListNode start = prev.next;
1317

1418
for (int i = 0; i < n - m; i++) {
19+
ListNode then = start.next;
20+
1521
start.next = then.next;
16-
then.next = begin.next;
17-
begin.next = then;
18-
then = start.next;
22+
then.next = prev.next;
23+
prev.next = then;
1924
}
2025

2126
return dummy.next;

0 commit comments

Comments
 (0)