forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReorder List.java
40 lines (35 loc) · 1.13 KB
/
Reorder List.java
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
class Solution {
public void reorderList(ListNode head) {
if (head == null) return;
// Find start of second list
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode list1 = head;
ListNode list2 = reverseList(slow.next); // slow.next is start of list2
// Break first list from second list!
slow.next = null;
// Merge list1 and list2
while (list2 != null) {
ListNode l1Next = list1.next;
ListNode l2Next = list2.next;
list2.next = list1.next;
list1.next = list2;
list1 = l1Next;
list2 = l2Next;
}
}
private ListNode reverseList(ListNode node) {
if (node == null) return node;
ListNode newHead = null, currNode = node;
while (currNode != null) {
ListNode backup = currNode.next;
currNode.next = newHead;
newHead = currNode;
currNode = backup;
}
return newHead;
}
}