// 执行耗时:0 ms,击败了100.00% 的Java用户
// 内存消耗:37.8 MB,击败了94.44% 的Java用户
class Solution {
public ListNode partition(ListNode head, int x) {
// 采用328.奇偶链表类似的思路
// 创建两个链表后再拼接
ListNode sentinel1 = new ListNode(0), sentinel2 = new ListNode(0);
ListNode curr1 = sentinel1, curr2 = sentinel2;
while (head != null){
if (head.val < x){
curr1.next = head;
curr1 = curr1.next;
} else {
curr2.next = head;
curr2 = curr2.next;
}
head = head.next;
}
curr1.next = sentinel2.next;
curr2.next = null;
return sentinel1.next;
}
}