86. Partition List
class Solution {
public ListNode partition(ListNode head, int x) {
ListNode dummySmall = new ListNode(0);
ListNode dummyLarge = new ListNode(0);
ListNode dS = dummySmall; //记录head
ListNode dL = dummyLarge; //记录head
while (head != null) {
if (head.val < x) {
dummySmall.next = head;
dummySmall = dummySmall.next;
} else {
dummyLarge.next = head;
dummyLarge = dummyLarge.next;
}
head = head.next;
}
dummySmall.next = dL.next;
dummyLarge.next = null;
return dS.next;
}
}Last updated