forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_86.java
185 lines (173 loc) · 7.12 KB
/
_86.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
/**
* Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.
*/
public class _86 {
public ListNode partition(ListNode head, int x) {
if(head == null){
return head;
}
else{
ListNode keeper, detector, detPre, keepNxt, myHead;/* initialize two pointers, "keeper" is used to
indicate where the next node that is smaller than x should be appended while
detector moves in the forefront to detect whether each node is smaller than x */
detector = head;
keeper = head;
detPre = head;
keepNxt = head;
myHead = new ListNode(Integer.MAX_VALUE);
myHead.next = head;
if(head.val >= x){
keeper = myHead;
/* we use two while loops:
* first one to locate where the initial position of keeper should be;
* second one to start traversing the whole linkedlist */
while(detector != null){
/* first while loop*/
if(detector.val < x){
detPre.next = detector.next;
keeper = detector;
keeper.next = head;
keepNxt = keeper.next;
myHead = keeper;
detector = detPre.next;
break;
}
else{
if(detector.next != null){
detPre = detector;
detector = detector.next;
}
else{
break;
}
}
}
while(detector != null){
/* second while loop */
if(detector.val >= x){
if(detector.next != null){
detPre = detector;
detector = detector.next;
}
else{
if(Integer.MAX_VALUE == myHead.val){
myHead = myHead.next;
}
else
break;
}
}
else{
if(detector.next != null){
detPre.next = detector.next;
keeper.next = detector;
keeper.next.next = keepNxt;
detector = detPre.next;
/* then I'll have to update the keeper pointer and keepNxt pointer*/
keeper = keeper.next;
keepNxt = keeper.next;
}
else{
keeper.next = detector;
keeper.next.next = keepNxt;
detPre.next = null;
break;
}
}
System.out.println("\nIn second while loop: keeper.val = " + keeper.val + "\tkeepNxt.val = " + keepNxt.val + "\tdetector.val = " + detector.val
+ "\tdetPre.val = " + detPre.val);
}
System.out.println();
ListNode temp = myHead;
while(temp != null){
System.out.print(temp.val);
temp = temp.next;
}
return myHead;
}
else{/* when the very first node is greater or equal than x */
/* we use two while loops:
* first one to locate where the initial position of keeper should be;
* second one to start traversing the whole linkedlist */
while(detector != null ){
/* first while loop*/
if(detector.val >= x){
break;
}
else{
keeper = detector;
detector = detector.next;
}
}
if(detector != null){
detPre = keeper;
keepNxt = keeper.next;
while(detector != null){
/* second while loop */
if(detector.val >= x){
if(detector.next != null){
detPre = detector;
detector = detector.next;
}
else{
break;
}
}
else{
if(detector.next != null){
detPre.next = detector.next;
keeper.next = detector;
keeper.next.next = keepNxt;
detector = detPre.next;
/* then I'll have to update the keeper pointer and keepNxt pointer*/
keeper = keeper.next;
keepNxt = keeper.next;
}
else{
keeper.next = detector;
keeper.next.next = keepNxt;
detPre.next = null;
break;
}
}
System.out.println("\nIn second while loop: keeper.val = " + keeper.val + "\tkeepNxt.val = " + keepNxt.val + "\tdetector.val = " + detector.val
+ "\tdetPre.val = " + detPre.val);
ListNode temp = head;
while(temp != null){
System.out.print(temp.val);
temp = temp.next;
}
}
System.out.println();
ListNode temp = head;
while(temp != null){
System.out.print(temp.val);
temp = temp.next;
}
return head;
}
else if(detector == null){
System.out.println("\nIn second while loop: keeper.val = " + keeper.val + "\tkeepNxt.val = " + keepNxt.val + "\tdetPre.val = " + detPre.val);
ListNode temp = head;
while(temp != null){
System.out.print(temp.val);
temp = temp.next;
}
return head;
}
}
ListNode temp = head;
while(temp != null){
System.out.print(temp.val);
temp = temp.next;
}
return head;
}
}
}