-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReorderList.java
99 lines (83 loc) · 2.3 KB
/
ReorderList.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*Created by @Kurt LEE On 1/12/23 11:07 PM.*/
import java.util.List;
public class ReorderList {
public static class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
// driver code
public static void main(String[] args) {
ListNode head = new ListNode(1);
ListNode node1 = new ListNode(2);
ListNode node2 = new ListNode(3);
ListNode node3 = new ListNode(4);
ListNode node4 = new ListNode(5);
head.next = node1;
node1.next = node2;
node2.next = node3;
node3.next = node4;
reorderList(head);
while (head != null) {
System.out.println(head.val);
head = head.next;
}
}
public static ListNode reorderList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode head2 = slow.next;
slow.next = null;
head2 = reverse(head2);
merge(head, head2);
return head;
}
public static ListNode reverse(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
public static void merge(ListNode head1, ListNode head2) {
ListNode dummy = new ListNode(0);
ListNode curr = dummy;
int index = 0;
while (head1 != null && head2 != null) {
if (index % 2 == 0) {
curr.next = head1;
head1 = head1.next;
} else {
curr.next = head2;
head2 = head2.next;
}
curr = curr.next;
index++;
}
if (head1 != null) {
curr.next = head1;
}
if (head2 != null) {
curr.next = head2;
}
}
}