/
Solution.java
executable file
·154 lines (129 loc) · 3.48 KB
/
Solution.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void reorderList(ListNode head) {
if(head == null || head.next == null) return;
ListNode fast = head,slow = head;
while(fast.next != null){
fast = fast.next;
if(fast.next != null) {
fast = fast.next;
slow = slow.next;
}
}
ListNode dummy = new ListNode(0);
dummy.next = slow.next;
slow.next = null;
ListNode cur = dummy.next;
while(cur.next != null){
ListNode tmp = cur.next.next;
cur.next.next = dummy.next;
dummy.next = cur.next;
cur.next = tmp;
}
cur = head;
while(dummy.next != null){
ListNode tmp = dummy.next.next;
dummy.next.next = cur.next;
cur.next = dummy.next;
dummy.next = tmp;
cur = cur.next.next;
}
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public void reorderList(ListNode head) {
if(head == null || head.next == null) return;
ListNode odd = new ListNode(0);
ListNode cur = head;
ListNode tmp;
int count = 0;
while(cur.next != null){
if(count%2 == 0){
tmp = cur.next.next;
cur.next.next = odd.next;
odd.next = cur.next;
cur.next = tmp;
}
cur = cur.next;
count++;
}
cur = head;
while(odd.next != null){
tmp = cur.next;
ListNode tmp2 = odd.next.next;
odd.next.next = cur.next;
cur.next = odd.next;
cur.next.next = tmp;
cur = tmp;
odd.next = tmp2;
}
}
}
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
ListNode reverse(ListNode head){
ListNode prev = null;
while(head != null){
ListNode t = head.next;
head.next = prev;
prev = head;
head = t;
}
return prev;
}
ListNode mid(ListNode head){
ListNode fast = head;
ListNode slow = head;
while(fast != null && fast.next != null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
public void reorderList(ListNode head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(head == null) return;
if(head.next == null) return;
ListNode left = head;
ListNode mid = mid(head);
ListNode right = mid.next;
if(right == null) return;
mid.next = null;
right = reverse(right);
while(head != null){
ListNode t = head.next;
ListNode r = right;
if(r == null) return;
head.next = r;
right = right.next;
r.next = t;
head = t;
}
}
}