Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5b52d20

Browse files
committed
Partition linkedlist done
1 parent c0cf56c commit 5b52d20

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.ctci.linkedlists;
2+
3+
import static com.ctci.linkedlists.Node.printList;
4+
5+
/**
6+
* Write code to partition a linked list around a value x, such that all nodes less than x come before all
7+
* nodes greater than or equal to x. If x is contained within the list, the values of x only need to be
8+
* after the elements less than x (see below). The partition element x can appear anywhere in the "right
9+
* partition"; it does not need to appear between the left and right partitions.
10+
* <p>
11+
* EXAMPLE:
12+
* Input: 3 -> 5 -> 8 -> 5 -> 10 -> 2 -> 1 [partition=5]
13+
* Output: 3 -> 1 -> 2 -> 10 -> 5 -> 5 -> 8
14+
*
15+
* @author rampatra
16+
* @since 2019-01-28
17+
*/
18+
public class Partition {
19+
20+
private static Node partition(Node head, int x) {
21+
Node leftPartitionHead = null;
22+
Node leftPartitionCurr = null;
23+
Node rightPartitionHead = null;
24+
Node rightPartitionCurr = null;
25+
Node curr = head;
26+
27+
while (curr != null) {
28+
if (curr.val < x) {
29+
if (leftPartitionHead == null) {
30+
leftPartitionHead = curr;
31+
leftPartitionCurr = curr;
32+
} else {
33+
leftPartitionCurr.next = curr;
34+
leftPartitionCurr = leftPartitionCurr.next;
35+
}
36+
} else {
37+
if (rightPartitionHead == null) {
38+
rightPartitionHead = curr;
39+
rightPartitionCurr = curr;
40+
} else {
41+
rightPartitionCurr.next = curr;
42+
rightPartitionCurr = rightPartitionCurr.next;
43+
}
44+
}
45+
curr = curr.next;
46+
}
47+
48+
if (leftPartitionCurr != null) leftPartitionCurr.next = rightPartitionHead;
49+
if (rightPartitionCurr != null) rightPartitionCurr.next = null;
50+
51+
return leftPartitionHead;
52+
}
53+
54+
public static void main(String[] args) {
55+
Node l1 = new Node(3);
56+
l1.next = new Node(5);
57+
l1.next.next = new Node(8);
58+
l1.next.next.next = new Node(5);
59+
l1.next.next.next.next = new Node(10);
60+
l1.next.next.next.next.next = new Node(2);
61+
l1.next.next.next.next.next.next = new Node(1);
62+
printList(l1);
63+
printList(partition(l1, 5));
64+
65+
System.out.println("----");
66+
67+
l1 = new Node(1);
68+
l1.next = new Node(2);
69+
l1.next.next = new Node(3);
70+
printList(l1);
71+
printList(partition(l1, 2));
72+
73+
System.out.println("----");
74+
75+
l1 = new Node(3);
76+
l1.next = new Node(2);
77+
l1.next.next = new Node(1);
78+
printList(l1);
79+
printList(partition(l1, 2));
80+
81+
System.out.println("----");
82+
83+
l1 = new Node(1);
84+
printList(l1);
85+
printList(partition(l1, 1));
86+
87+
System.out.println("----");
88+
89+
l1 = null;
90+
printList(l1);
91+
printList(partition(l1, 1));
92+
93+
System.out.println("----");
94+
}
95+
}

0 commit comments

Comments
 (0)