-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReorderListSolution.java
71 lines (66 loc) · 1.86 KB
/
ReorderListSolution.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
import java.util.*;
public class ReorderListSolution {
public static class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
this.next = null;
}
}
public static void print(ListNode head){
while(head != null){
System.out.println(head.val);
head = head.next;
}
}
public static void reorderList(ListNode head){
if(head == null || head.next == null){
return;
}
ListNode fast = head;
ListNode slow = head;
while(fast.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;
}
ListNode list1 = head;
ListNode list2 = slow.next;
slow.next = null;
//reverse list2
ListNode list2Pre = null;
ListNode list2Aft = list2;
while(list2Aft != null){
ListNode temp = list2Aft.next;
list2Aft.next = list2Pre;
list2Pre = list2Aft;
list2Aft = temp;
}
//merge two lists
list2 = list2Pre;
print(list1);
print(list2);
ListNode p = list1;
ListNode q = list2;
while(p != null && q != null){
ListNode temp1 = p.next;
ListNode temp2 = q.next;
p.next = q;
q.next = temp1;
p = temp1;
q = temp2;
}
}
public static void main(String args[]){
//int[] A = {-3,1,1,-3,5};
//ArrayList<Integer> A = new ArrayList<Integer>();
ListNode head = new ListNode(2);
ListNode node1 = new ListNode(-1);
ListNode node2 = new ListNode(0);
head.next = node1;
node1.next = node2;
node2.next = null;
//Integer a = new Integer(1);
reorderList(head);
}
}