forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartitionList.java
More file actions
50 lines (45 loc) · 1.22 KB
/
PartitionList.java
File metadata and controls
50 lines (45 loc) · 1.22 KB
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
package LinkedLists;
/**
* Author - archit.s
* Date - 20/10/18
* Time - 9:09 PM
*/
public class PartitionList {
public ListNode partition(ListNode A, int B) {
ListNode smaller = null;
ListNode smallerLast = null;
ListNode larger = null;
ListNode largerLast = null;
ListNode current = A;
while(current!=null){
if(current.val < B){
if(smaller == null){
smaller = current;
smallerLast = smaller;
}
else{
smallerLast.next = current;
smallerLast = smallerLast.next;
}
}
else{
if(larger == null){
larger = current;
largerLast = larger;
}
else{
largerLast.next = current;
largerLast = largerLast.next;
}
}
current = current.next;
}
if(smaller!=null){
smallerLast.next = larger;
largerLast.next = null;
return smaller;
}
largerLast.next = null;
return larger;
}
}