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
| class Solution { public ListNode partition(ListNode head, int x) { ListNode dummyLeftHead = new ListNode(); ListNode dummyRightHead = new ListNode();
ListNode leftNode = dummyLeftHead; ListNode rightNode = dummyRightHead; while (head != null) { if (head.val < x) { leftNode.next = head; leftNode = leftNode.next; } else { rightNode.next = head; rightNode = rightNode.next; }
head = head.next; }
leftNode.next = dummyRightHead.next; rightNode.next = null;
return dummyLeftHead.next; } }
|