Skip to content

Commit e294a1d

Browse files
committed
solve recorder list
1 parent 2367085 commit e294a1d

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

reorder-list/sora0319.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public class Solution {
2+
public void reorderList(ListNode head) {
3+
if (head == null || head.next == null) return;
4+
5+
ListNode backward = head;
6+
ListNode forward = head;
7+
while (forward != null && forward.next != null) {
8+
backward = backward.next;
9+
forward = forward.next.next;
10+
}
11+
12+
ListNode curr = backward.next;
13+
backward.next = null;
14+
15+
ListNode prev = null;
16+
while (curr != null) {
17+
ListNode tempNext = curr.next;
18+
curr.next = prev;
19+
prev = curr;
20+
curr = tempNext;
21+
}
22+
23+
ListNode first = head;
24+
ListNode second = prev;
25+
26+
while (second != null) {
27+
ListNode firstNext = first.next;
28+
ListNode secondNext = second.next;
29+
30+
first.next = second;
31+
second.next = firstNext;
32+
33+
first = firstNext;
34+
second = secondNext;
35+
}
36+
}
37+
}
38+

0 commit comments

Comments
 (0)