-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPartitionList.java
More file actions
41 lines (33 loc) · 976 Bytes
/
Copy pathPartitionList.java
File metadata and controls
41 lines (33 loc) · 976 Bytes
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
/**
* url: https://leetcode.com/problems/partition-list/
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode lessHead = new ListNode(0);
ListNode equalHead = new ListNode(0);
ListNode greaterHead = new ListNode(0);
ListNode lessIter = lessHead;
ListNode equalIter = equalHead;
ListNode greaterIter = greaterHead;
ListNode iter = head;
while (iter != null) {
if (iter.val < x) {
lessIter.next = iter;
lessIter = iter;
} else {
greaterIter.next = iter;
greaterIter = iter;
}
iter = iter.next;
}
greaterIter.next = null;
lessIter.next = greaterHead.next;
return lessHead.next;
}
}