-
-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathReorderList.java
59 lines (51 loc) · 1.6 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
package java1.algorithms.linkedlist;
public class ReorderList {
// TC: O(n) SC: O(1)
private static Node reorderList(Node head) {
Node slow = head, fast = head.next;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
Node secondHalf = slow.next;
Node prev = slow.next = null;
//Reverse the second partition
while(secondHalf != null) {
Node temp = secondHalf.next;
secondHalf.next = prev;
prev = secondHalf;
secondHalf = temp;
}
Node firstHalf = head;
secondHalf = prev;
//Re-order the list
while(secondHalf != null) {
Node temp1 = firstHalf.next, temp2 = secondHalf.next;
firstHalf.next = secondHalf;
secondHalf.next = temp1;
secondHalf = temp2;
firstHalf = temp1;
}
return head;
}
private static void printLinkedlist(Node head) {
while(head != null) {
System.out.println(head.value + " ");
head = head.next;
}
}
public static void main(String[] args) {
Node headNode1 = new Node(10);
Node node2 = new Node(20);
Node node3 = new Node(30);
Node node4 = new Node(40);
Node node5 = new Node(50);
Node node6 = new Node(60);
headNode1.setNext(node2);
node2.setNext(node3);
node3.setNext(node4);
node4.setNext(node5);
node5.setNext(node6);
printLinkedlist(reorderList(headNode1));
}
}