Skip to content

Commit

Permalink
Merge pull request #356 from Xuewei-Chen/main
Browse files Browse the repository at this point in the history
PartitionList
  • Loading branch information
Twiggecode authored Mar 12, 2022
2 parents 2fe39e3 + b0e2ffd commit bab746f
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Partition Numbers/PartitionList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//This algorithm is to partition it such that all nodes less than x come before nodes greater than or equal to x.

import java.io.*;

class Solution{
public ListNode partition(ListNode head, int x){
ListNode small = new ListNode(0);
ListNode large = new ListNode(0);

ListNode p1 = small;
ListNode p2 = large;

while(head != null){
if(head.val < x){
p1.next = head;
p1 = p1.next;
}else{
p2.next = head;
p2 = p2.next;
}
head = head.next;
}
p2.next = null;
p1.next = large.next;
return small.next;
}
}

0 comments on commit bab746f

Please sign in to comment.