@Test public void testIntersection() { LinkedList<Integer> l1 = newLinkedList(2, 3, 5, 7); LinkedList<Integer> l2 = newLinkedList(1, 5, 7); LinkedListNode<Integer> intersection = intersection(l1, l2); Assert.assertNotNull(intersection); Assert.assertEquals(5, intersection.data().intValue()); }
public static LinkedListNode<Integer> intersection(LinkedList<Integer> l1, LinkedList<Integer> l2) { final int sizeL1 = l1.size(); final int sizeL2 = l2.size(); LOGGER.info("List 1 size: {}.", sizeL1); LOGGER.info("List 2 size: {}.", sizeL2); int diff = sizeL1 - sizeL2; LinkedListNode<Integer> l1Pointer = l1.head(); LinkedListNode<Integer> l2Pointer = l2.head(); if (diff < 0) { for (; diff > 0; l2Pointer = l2Pointer.successor(), --diff) ; } else { for (; diff > 0; l1Pointer = l1Pointer.successor(), --diff) ; } while (l1Pointer != l1.tail() && l2Pointer != l2.tail()) { LOGGER.info("List 1 pointer is at: {}.", l1Pointer); LOGGER.info("List 2 pointer is at: {}.", l2Pointer); if (l1Pointer.equals(l2Pointer)) { return l1Pointer; } l1Pointer = l1Pointer.successor(); l2Pointer = l2Pointer.successor(); } return null; }
PracticeQuestionsCh7 { public static LinkedListNode<Integer> intersection(LinkedList<Integer> l1, LinkedList<Integer> l2) { final int sizeL1 = l1.size(); final int sizeL2 = l2.size(); LOGGER.info("List 1 size: {}.", sizeL1); LOGGER.info("List 2 size: {}.", sizeL2); int diff = sizeL1 - sizeL2; LinkedListNode<Integer> l1Pointer = l1.head(); LinkedListNode<Integer> l2Pointer = l2.head(); if (diff < 0) { for (; diff > 0; l2Pointer = l2Pointer.successor(), --diff) ; } else { for (; diff > 0; l1Pointer = l1Pointer.successor(), --diff) ; } while (l1Pointer != l1.tail() && l2Pointer != l2.tail()) { LOGGER.info("List 1 pointer is at: {}.", l1Pointer); LOGGER.info("List 2 pointer is at: {}.", l2Pointer); if (l1Pointer.equals(l2Pointer)) { return l1Pointer; } l1Pointer = l1Pointer.successor(); l2Pointer = l2Pointer.successor(); } return null; } }
PracticeQuestionsCh7 { public static LinkedListNode<Integer> intersection(LinkedList<Integer> l1, LinkedList<Integer> l2) { final int sizeL1 = l1.size(); final int sizeL2 = l2.size(); LOGGER.info("List 1 size: {}.", sizeL1); LOGGER.info("List 2 size: {}.", sizeL2); int diff = sizeL1 - sizeL2; LinkedListNode<Integer> l1Pointer = l1.head(); LinkedListNode<Integer> l2Pointer = l2.head(); if (diff < 0) { for (; diff > 0; l2Pointer = l2Pointer.successor(), --diff) ; } else { for (; diff > 0; l1Pointer = l1Pointer.successor(), --diff) ; } while (l1Pointer != l1.tail() && l2Pointer != l2.tail()) { LOGGER.info("List 1 pointer is at: {}.", l1Pointer); LOGGER.info("List 2 pointer is at: {}.", l2Pointer); if (l1Pointer.equals(l2Pointer)) { return l1Pointer; } l1Pointer = l1Pointer.successor(); l2Pointer = l2Pointer.successor(); } return null; }  }
PracticeQuestionsCh7 { public static LinkedListNode<Integer> intersection(LinkedList<Integer> l1, LinkedList<Integer> l2) { final int sizeL1 = l1.size(); final int sizeL2 = l2.size(); LOGGER.info("List 1 size: {}.", sizeL1); LOGGER.info("List 2 size: {}.", sizeL2); int diff = sizeL1 - sizeL2; LinkedListNode<Integer> l1Pointer = l1.head(); LinkedListNode<Integer> l2Pointer = l2.head(); if (diff < 0) { for (; diff > 0; l2Pointer = l2Pointer.successor(), --diff) ; } else { for (; diff > 0; l1Pointer = l1Pointer.successor(), --diff) ; } while (l1Pointer != l1.tail() && l2Pointer != l2.tail()) { LOGGER.info("List 1 pointer is at: {}.", l1Pointer); LOGGER.info("List 2 pointer is at: {}.", l2Pointer); if (l1Pointer.equals(l2Pointer)) { return l1Pointer; } l1Pointer = l1Pointer.successor(); l2Pointer = l2Pointer.successor(); } return null; }  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> intersection(LinkedList<Integer> l1, LinkedList<Integer> l2) { final int sizeL1 = l1.size(); final int sizeL2 = l2.size(); LOGGER.info("List 1 size: {}.", sizeL1); LOGGER.info("List 2 size: {}.", sizeL2); int diff = sizeL1 - sizeL2; LinkedListNode<Integer> l1Pointer = l1.head(); LinkedListNode<Integer> l2Pointer = l2.head(); if (diff < 0) { for (; diff > 0; l2Pointer = l2Pointer.successor(), --diff) ; } else { for (; diff > 0; l1Pointer = l1Pointer.successor(), --diff) ; } while (l1Pointer != l1.tail() && l2Pointer != l2.tail()) { LOGGER.info("List 1 pointer is at: {}.", l1Pointer); LOGGER.info("List 2 pointer is at: {}.", l2Pointer); if (l1Pointer.equals(l2Pointer)) { return l1Pointer; } l1Pointer = l1Pointer.successor(); l2Pointer = l2Pointer.successor(); } return null; }  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; }