@Test public void testPartition() { LinkedList<Integer> linkedList = new LinkedList<Integer>(); linkedList.add(2); linkedList.add(4); linkedList.add(5); linkedList.add(1); partition(linkedList, 4); assertEquals(4, linkedList.size()); assertEquals(Integer.valueOf(2), linkedList.get(0)); assertEquals(Integer.valueOf(1), linkedList.get(1)); assertEquals(Integer.valueOf(4), linkedList.get(2)); assertEquals(Integer.valueOf(5), linkedList.get(3)); }
public static <E extends Comparable<E>> void partition(LinkedList<E> linkedList, E pivot) { int lastIdx = linkedList.size() - 1; int pivotIdx = linkedList.indexOf(pivot); if (pivotIdx != linkedList.lastIndexOf(pivot)) { throw new IllegalArgumentException("Can't handle dupes in input list."); } swap(linkedList, pivotIdx, lastIdx); int storeIdx = 0; for (int runningIdx = 0; runningIdx < lastIdx; runningIdx++) { if (linkedList.get(runningIdx).compareTo(pivot) <= 0) { swap(linkedList, runningIdx, storeIdx); storeIdx++; } } swap(linkedList, storeIdx, lastIdx); }
PracticeQuestionsCh2 { public static <E extends Comparable<E>> void partition(LinkedList<E> linkedList, E pivot) { int lastIdx = linkedList.size() - 1; int pivotIdx = linkedList.indexOf(pivot); if (pivotIdx != linkedList.lastIndexOf(pivot)) { throw new IllegalArgumentException("Can't handle dupes in input list."); } swap(linkedList, pivotIdx, lastIdx); int storeIdx = 0; for (int runningIdx = 0; runningIdx < lastIdx; runningIdx++) { if (linkedList.get(runningIdx).compareTo(pivot) <= 0) { swap(linkedList, runningIdx, storeIdx); storeIdx++; } } swap(linkedList, storeIdx, lastIdx); } }
PracticeQuestionsCh2 { public static <E extends Comparable<E>> void partition(LinkedList<E> linkedList, E pivot) { int lastIdx = linkedList.size() - 1; int pivotIdx = linkedList.indexOf(pivot); if (pivotIdx != linkedList.lastIndexOf(pivot)) { throw new IllegalArgumentException("Can't handle dupes in input list."); } swap(linkedList, pivotIdx, lastIdx); int storeIdx = 0; for (int runningIdx = 0; runningIdx < lastIdx; runningIdx++) { if (linkedList.get(runningIdx).compareTo(pivot) <= 0) { swap(linkedList, runningIdx, storeIdx); storeIdx++; } } swap(linkedList, storeIdx, lastIdx); }  }
PracticeQuestionsCh2 { public static <E extends Comparable<E>> void partition(LinkedList<E> linkedList, E pivot) { int lastIdx = linkedList.size() - 1; int pivotIdx = linkedList.indexOf(pivot); if (pivotIdx != linkedList.lastIndexOf(pivot)) { throw new IllegalArgumentException("Can't handle dupes in input list."); } swap(linkedList, pivotIdx, lastIdx); int storeIdx = 0; for (int runningIdx = 0; runningIdx < lastIdx; runningIdx++) { if (linkedList.get(runningIdx).compareTo(pivot) <= 0) { swap(linkedList, runningIdx, storeIdx); storeIdx++; } } swap(linkedList, storeIdx, lastIdx); }  static void removeDupes(LinkedList<E> linkedList); static void partition(LinkedList<E> linkedList, E pivot); static boolean isPalindrome(LinkedList<E> linkedList); static boolean isPalindromeVariant(LinkedList<E> linkedList); }
PracticeQuestionsCh2 { public static <E extends Comparable<E>> void partition(LinkedList<E> linkedList, E pivot) { int lastIdx = linkedList.size() - 1; int pivotIdx = linkedList.indexOf(pivot); if (pivotIdx != linkedList.lastIndexOf(pivot)) { throw new IllegalArgumentException("Can't handle dupes in input list."); } swap(linkedList, pivotIdx, lastIdx); int storeIdx = 0; for (int runningIdx = 0; runningIdx < lastIdx; runningIdx++) { if (linkedList.get(runningIdx).compareTo(pivot) <= 0) { swap(linkedList, runningIdx, storeIdx); storeIdx++; } } swap(linkedList, storeIdx, lastIdx); }  static void removeDupes(LinkedList<E> linkedList); static void partition(LinkedList<E> linkedList, E pivot); static boolean isPalindrome(LinkedList<E> linkedList); static boolean isPalindromeVariant(LinkedList<E> linkedList);  }