We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 23bfa4a commit 8a57257Copy full SHA for 8a57257
In-place Reveral of a Linked List/92_Reverse_Linked_List_II.java
@@ -1,21 +1,26 @@
1
class Solution {
2
public ListNode reverseBetween(ListNode head, int m, int n) {
3
+ if (head == null) {
4
+ return null;
5
+ }
6
+
7
ListNode dummy = new ListNode(-1);
8
dummy.next = head;
9
- ListNode begin = dummy;
10
+ ListNode prev = dummy;
11
12
for (int i = 0; i < m - 1; i++) {
- begin = begin.next;
13
+ prev = prev.next;
14
}
15
- ListNode start = begin.next, then = start.next;
16
+ ListNode start = prev.next;
17
18
for (int i = 0; i < n - m; i++) {
19
+ ListNode then = start.next;
20
21
start.next = then.next;
- then.next = begin.next;
- begin.next = then;
- then = start.next;
22
+ then.next = prev.next;
23
+ prev.next = then;
24
25
26
return dummy.next;
0 commit comments