class Solution {
public ListNode partition(ListNode head, int x) {
ListNode left = new ListNode(-101), right = new ListNode(-101);
ListNode leftHead = left, rightHead = right;
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;
}
}