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 26 27 28 29 30 31 32 33 34 35 36
| class Solution { public ListNode partition(ListNode head, int x) { ListNode tmp = head; ListNode rootMin = null; ListNode rootMax = null; ListNode min = null; ListNode max = null; while (tmp != null) { if (tmp.val < x){ if (min == null) { min = new ListNode(tmp.val); rootMin = min; }else { min.next = new ListNode(tmp.val); min = min.next; } }else { if (max == null){ max = new ListNode(tmp.val); rootMax = max; }else { max.next = new ListNode(tmp.val); max = max.next; } } tmp = tmp.next; }
if (min == null) { return rootMax; }else { min.next = rootMax; return rootMin; } } }
|