@Test public void testMergeLists() { LinkedList<Integer> l = newLinkedList(2, 5, 7); LinkedList<Integer> f = newLinkedList(3, 11); int[] merged = new int[] { 2, 3, 5, 7, 11 }; LinkedListNode<Integer> runningNode = mergeLists(l, f); assertArrayEqualsList(merged, runningNode); l = newLinkedList(3, 11); f = newLinkedList(2, 5, 7); runningNode = mergeLists(l, f); assertArrayEqualsList(merged, runningNode); }
public static LinkedListNode<Integer> mergeLists(LinkedList<Integer> l, LinkedList<Integer> f) { final String newline = System.getProperty("line.separator"); LinkedListNode<Integer> currentHead = l.head(); LinkedListNode<Integer> bigger = currentHead.successor(); LinkedListNode<Integer> otherHead = f.head(); LinkedListNode<Integer> smallest = currentHead.successor(); while (otherHead.successor().data() != null) { LOGGER.info("{}", newline); LOGGER.info("Current head: {}.", currentHead); LOGGER.info("Current: {}.", bigger); LOGGER.info("Other head: {}.", otherHead); LOGGER.info("Smallest: {}.", smallest); if (bigger.data() < otherHead.successor().data()) { LOGGER.info("Setting current head successor to: {}.", bigger.successor()); currentHead.setSuccessor(bigger.successor()); LOGGER.info("Setting bigger successor to: {}.", otherHead.successor()); bigger.setSuccessor(otherHead.successor()); LOGGER.info("Setting bigger to: {}.", otherHead.successor()); bigger = otherHead.successor(); LOGGER.info("Setting other head successor to: {}.", smallest); otherHead.setSuccessor(smallest); LOGGER.info("Swapping heads."); LinkedListNode<Integer> tempHead = currentHead; currentHead = otherHead; otherHead = tempHead; } else { LinkedListNode<Integer> predecessor = predecessor(currentHead, bigger); LOGGER.info("Predecessor to {} is {}.", bigger, predecessor); LinkedListNode<Integer> temp = otherHead.successor(); LOGGER.info("Setting predecessor successor to: {}.", temp); predecessor.setSuccessor(temp); LOGGER.info("Setting smallest to: {}.", currentHead.successor()); smallest = currentHead.successor(); LOGGER.info("Setting other head successor to: {}.", temp.successor()); otherHead.setSuccessor(temp.successor()); LOGGER.info("Setting temp successor to: {}.", bigger); temp.setSuccessor(bigger); } } return currentHead; }
PracticeQuestionsCh7 { public static LinkedListNode<Integer> mergeLists(LinkedList<Integer> l, LinkedList<Integer> f) { final String newline = System.getProperty("line.separator"); LinkedListNode<Integer> currentHead = l.head(); LinkedListNode<Integer> bigger = currentHead.successor(); LinkedListNode<Integer> otherHead = f.head(); LinkedListNode<Integer> smallest = currentHead.successor(); while (otherHead.successor().data() != null) { LOGGER.info("{}", newline); LOGGER.info("Current head: {}.", currentHead); LOGGER.info("Current: {}.", bigger); LOGGER.info("Other head: {}.", otherHead); LOGGER.info("Smallest: {}.", smallest); if (bigger.data() < otherHead.successor().data()) { LOGGER.info("Setting current head successor to: {}.", bigger.successor()); currentHead.setSuccessor(bigger.successor()); LOGGER.info("Setting bigger successor to: {}.", otherHead.successor()); bigger.setSuccessor(otherHead.successor()); LOGGER.info("Setting bigger to: {}.", otherHead.successor()); bigger = otherHead.successor(); LOGGER.info("Setting other head successor to: {}.", smallest); otherHead.setSuccessor(smallest); LOGGER.info("Swapping heads."); LinkedListNode<Integer> tempHead = currentHead; currentHead = otherHead; otherHead = tempHead; } else { LinkedListNode<Integer> predecessor = predecessor(currentHead, bigger); LOGGER.info("Predecessor to {} is {}.", bigger, predecessor); LinkedListNode<Integer> temp = otherHead.successor(); LOGGER.info("Setting predecessor successor to: {}.", temp); predecessor.setSuccessor(temp); LOGGER.info("Setting smallest to: {}.", currentHead.successor()); smallest = currentHead.successor(); LOGGER.info("Setting other head successor to: {}.", temp.successor()); otherHead.setSuccessor(temp.successor()); LOGGER.info("Setting temp successor to: {}.", bigger); temp.setSuccessor(bigger); } } return currentHead; } }
PracticeQuestionsCh7 { public static LinkedListNode<Integer> mergeLists(LinkedList<Integer> l, LinkedList<Integer> f) { final String newline = System.getProperty("line.separator"); LinkedListNode<Integer> currentHead = l.head(); LinkedListNode<Integer> bigger = currentHead.successor(); LinkedListNode<Integer> otherHead = f.head(); LinkedListNode<Integer> smallest = currentHead.successor(); while (otherHead.successor().data() != null) { LOGGER.info("{}", newline); LOGGER.info("Current head: {}.", currentHead); LOGGER.info("Current: {}.", bigger); LOGGER.info("Other head: {}.", otherHead); LOGGER.info("Smallest: {}.", smallest); if (bigger.data() < otherHead.successor().data()) { LOGGER.info("Setting current head successor to: {}.", bigger.successor()); currentHead.setSuccessor(bigger.successor()); LOGGER.info("Setting bigger successor to: {}.", otherHead.successor()); bigger.setSuccessor(otherHead.successor()); LOGGER.info("Setting bigger to: {}.", otherHead.successor()); bigger = otherHead.successor(); LOGGER.info("Setting other head successor to: {}.", smallest); otherHead.setSuccessor(smallest); LOGGER.info("Swapping heads."); LinkedListNode<Integer> tempHead = currentHead; currentHead = otherHead; otherHead = tempHead; } else { LinkedListNode<Integer> predecessor = predecessor(currentHead, bigger); LOGGER.info("Predecessor to {} is {}.", bigger, predecessor); LinkedListNode<Integer> temp = otherHead.successor(); LOGGER.info("Setting predecessor successor to: {}.", temp); predecessor.setSuccessor(temp); LOGGER.info("Setting smallest to: {}.", currentHead.successor()); smallest = currentHead.successor(); LOGGER.info("Setting other head successor to: {}.", temp.successor()); otherHead.setSuccessor(temp.successor()); LOGGER.info("Setting temp successor to: {}.", bigger); temp.setSuccessor(bigger); } } return currentHead; }  }
PracticeQuestionsCh7 { public static LinkedListNode<Integer> mergeLists(LinkedList<Integer> l, LinkedList<Integer> f) { final String newline = System.getProperty("line.separator"); LinkedListNode<Integer> currentHead = l.head(); LinkedListNode<Integer> bigger = currentHead.successor(); LinkedListNode<Integer> otherHead = f.head(); LinkedListNode<Integer> smallest = currentHead.successor(); while (otherHead.successor().data() != null) { LOGGER.info("{}", newline); LOGGER.info("Current head: {}.", currentHead); LOGGER.info("Current: {}.", bigger); LOGGER.info("Other head: {}.", otherHead); LOGGER.info("Smallest: {}.", smallest); if (bigger.data() < otherHead.successor().data()) { LOGGER.info("Setting current head successor to: {}.", bigger.successor()); currentHead.setSuccessor(bigger.successor()); LOGGER.info("Setting bigger successor to: {}.", otherHead.successor()); bigger.setSuccessor(otherHead.successor()); LOGGER.info("Setting bigger to: {}.", otherHead.successor()); bigger = otherHead.successor(); LOGGER.info("Setting other head successor to: {}.", smallest); otherHead.setSuccessor(smallest); LOGGER.info("Swapping heads."); LinkedListNode<Integer> tempHead = currentHead; currentHead = otherHead; otherHead = tempHead; } else { LinkedListNode<Integer> predecessor = predecessor(currentHead, bigger); LOGGER.info("Predecessor to {} is {}.", bigger, predecessor); LinkedListNode<Integer> temp = otherHead.successor(); LOGGER.info("Setting predecessor successor to: {}.", temp); predecessor.setSuccessor(temp); LOGGER.info("Setting smallest to: {}.", currentHead.successor()); smallest = currentHead.successor(); LOGGER.info("Setting other head successor to: {}.", temp.successor()); otherHead.setSuccessor(temp.successor()); LOGGER.info("Setting temp successor to: {}.", bigger); temp.setSuccessor(bigger); } } return currentHead; }  static LinkedListNode<Integer> mergeLists(LinkedList<Integer> l, LinkedList<Integer> f); static LinkedListNode<Integer> startOfLoop(LinkedList<Integer> l); static LinkedListNode<Integer> intersection(LinkedList<Integer> l1, LinkedList<Integer> l2); static void evenOddMerge(LinkedList<Integer> l); static void reverse(LinkedList<Integer> l); }
PracticeQuestionsCh7 { public static LinkedListNode<Integer> mergeLists(LinkedList<Integer> l, LinkedList<Integer> f) { final String newline = System.getProperty("line.separator"); LinkedListNode<Integer> currentHead = l.head(); LinkedListNode<Integer> bigger = currentHead.successor(); LinkedListNode<Integer> otherHead = f.head(); LinkedListNode<Integer> smallest = currentHead.successor(); while (otherHead.successor().data() != null) { LOGGER.info("{}", newline); LOGGER.info("Current head: {}.", currentHead); LOGGER.info("Current: {}.", bigger); LOGGER.info("Other head: {}.", otherHead); LOGGER.info("Smallest: {}.", smallest); if (bigger.data() < otherHead.successor().data()) { LOGGER.info("Setting current head successor to: {}.", bigger.successor()); currentHead.setSuccessor(bigger.successor()); LOGGER.info("Setting bigger successor to: {}.", otherHead.successor()); bigger.setSuccessor(otherHead.successor()); LOGGER.info("Setting bigger to: {}.", otherHead.successor()); bigger = otherHead.successor(); LOGGER.info("Setting other head successor to: {}.", smallest); otherHead.setSuccessor(smallest); LOGGER.info("Swapping heads."); LinkedListNode<Integer> tempHead = currentHead; currentHead = otherHead; otherHead = tempHead; } else { LinkedListNode<Integer> predecessor = predecessor(currentHead, bigger); LOGGER.info("Predecessor to {} is {}.", bigger, predecessor); LinkedListNode<Integer> temp = otherHead.successor(); LOGGER.info("Setting predecessor successor to: {}.", temp); predecessor.setSuccessor(temp); LOGGER.info("Setting smallest to: {}.", currentHead.successor()); smallest = currentHead.successor(); LOGGER.info("Setting other head successor to: {}.", temp.successor()); otherHead.setSuccessor(temp.successor()); LOGGER.info("Setting temp successor to: {}.", bigger); temp.setSuccessor(bigger); } } return currentHead; }  static LinkedListNode<Integer> mergeLists(LinkedList<Integer> l, LinkedList<Integer> f); static LinkedListNode<Integer> startOfLoop(LinkedList<Integer> l); static LinkedListNode<Integer> intersection(LinkedList<Integer> l1, LinkedList<Integer> l2); static void evenOddMerge(LinkedList<Integer> l); static void reverse(LinkedList<Integer> l); static final Logger LOGGER; }