-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy path45--reorder-list.java
More file actions
36 lines (34 loc) · 1.02 KB
/
Copy path45--reorder-list.java
File metadata and controls
36 lines (34 loc) · 1.02 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
class Solution {
public ListNode reverse(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode tmp = curr.next;
curr.next = prev;
prev = curr;
curr = tmp;
}
return prev;
}
public void reorderList(ListNode head) {
if (head == null || head.next == null) return;
ListNode slow = head, fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode head2 = slow.next;
slow.next = null;
ListNode head1 = head;
head2 = reverse(head2);
ListNode curr = new ListNode(-1);
while (head1 != null) {
ListNode tmp = head1.next;
curr.next = head1;
head1.next = head2;
curr = head2;
head1 = tmp;
if (head2 != null) head2 = head2.next;
}
}
} // TC: O(n), SC: O(1)