@Test public void shouldNotSplitListIfXIsGreaterThanEveryNode() { ListNode<Integer> list = createList(new int[] { 1, 2, 3 }); ListNode<Integer> result = partitionList.split(list, 4); assertListContainsElements(new Integer[] { 3, 2, 1 }, result); }
public ListNode<Integer> split(ListNode<Integer> list, int x) { validateInput(list); ListNode<Integer> before = null; ListNode<Integer> after = null; while (list != null) { ListNode<Integer> next = list.getNext(); if (list.getData() < x) { list.setNext(before); before = list; } else { list.setNext(after); after = list; } list = next; } if (before == null) { return after; } ListNode<Integer> result = mergeLists(before, after); return result; }
PartitionList { public ListNode<Integer> split(ListNode<Integer> list, int x) { validateInput(list); ListNode<Integer> before = null; ListNode<Integer> after = null; while (list != null) { ListNode<Integer> next = list.getNext(); if (list.getData() < x) { list.setNext(before); before = list; } else { list.setNext(after); after = list; } list = next; } if (before == null) { return after; } ListNode<Integer> result = mergeLists(before, after); return result; } }
PartitionList { public ListNode<Integer> split(ListNode<Integer> list, int x) { validateInput(list); ListNode<Integer> before = null; ListNode<Integer> after = null; while (list != null) { ListNode<Integer> next = list.getNext(); if (list.getData() < x) { list.setNext(before); before = list; } else { list.setNext(after); after = list; } list = next; } if (before == null) { return after; } ListNode<Integer> result = mergeLists(before, after); return result; }  }
PartitionList { public ListNode<Integer> split(ListNode<Integer> list, int x) { validateInput(list); ListNode<Integer> before = null; ListNode<Integer> after = null; while (list != null) { ListNode<Integer> next = list.getNext(); if (list.getData() < x) { list.setNext(before); before = list; } else { list.setNext(after); after = list; } list = next; } if (before == null) { return after; } ListNode<Integer> result = mergeLists(before, after); return result; }  ListNode<Integer> split(ListNode<Integer> list, int x); }
PartitionList { public ListNode<Integer> split(ListNode<Integer> list, int x) { validateInput(list); ListNode<Integer> before = null; ListNode<Integer> after = null; while (list != null) { ListNode<Integer> next = list.getNext(); if (list.getData() < x) { list.setNext(before); before = list; } else { list.setNext(after); after = list; } list = next; } if (before == null) { return after; } ListNode<Integer> result = mergeLists(before, after); return result; }  ListNode<Integer> split(ListNode<Integer> list, int x);  }