-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0086.java
More file actions
40 lines (34 loc) · 1.11 KB
/
_0086.java
File metadata and controls
40 lines (34 loc) · 1.11 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
package com.github.aditya;
public class _0086 {
// 0 ms, faster than 100.00%, memory 42.1 MB, less than 76.99%
// Time Complexity O(n) and Space Complexity O(1)
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode leftHead = new ListNode();
ListNode rightHead = new ListNode();
ListNode left = leftHead;
ListNode right = rightHead;
while (head != null) {
if (head.val < x) {
left.next = head;
left = left.next;
} else {
right.next = head;
right = right.next;
}
head = head.next;
}
left.next = rightHead.next;
right.next = null;
return leftHead.next;
}
}
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
}