1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public ListNode partition(ListNode head, int x) { if (head == null || head.next == null) { return head; } ListNode list1 = new ListNode(0); ListNode list2 = new ListNode(0); ListNode ptr1 = list1, ptr2 = list2, ptr3 = head; while (ptr3 != null) { if (ptr3.val < x) { ptr1.next = ptr3; ptr1 = ptr1.next; } else { ptr2.next = ptr3; ptr2 = ptr2.next; } ptr3 = ptr3.next; } ptr1.next = list2.next; ptr2.next = null; return list1.next; } }
|