-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path86. Partition List.java
More file actions
22 lines (22 loc) · 868 Bytes
/
Copy path86. Partition List.java
File metadata and controls
22 lines (22 loc) · 868 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//https://leetcode.com/problems/partition-list/
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode current = head;
ListNode lessDummy = new ListNode(0);
ListNode lessTail = lessDummy;
ListNode greaterDummy = new ListNode(0);
ListNode greaterTail = greaterDummy;
while (current != null) {
if (current.val < x) {
lessTail.next = new ListNode(current.val);
lessTail = lessTail.next; // Move the tail pointer
} else {
greaterTail.next = new ListNode(current.val);
greaterTail = greaterTail.next; // Move the tail pointer
}
current = current.next; // Move to the next node
}
lessTail.next = greaterDummy.next;
return lessDummy.next;
}
}