
| The Intersection Point Of Two Linked List: Using Difference of Node Count |
| class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class LinkedListIntersection { public static int findIntersection(ListNode headA, ListNode headB) { int lenA = getLength(headA); int lenB = getLength(headB); int diff = Math.abs(lenA - lenB); if (lenA > lenB) { while (diff > 0) { headA = headA.next; diff--; } } else { while (diff > 0) { headB = headB.next; diff--; } } while (headA!= null && headB!= null) { if (headA == headB) { return headA.val; } headA = headA.next; headB = headB.next; } return -1; } private static int getLength(ListNode head) { int length = 0; while (head!= null) { length++; head = head.next; } return length; } |
| Intersection of Linked List: Main( ) |
| public static void main(String[] args) { ListNode listA = new ListNode (1); listA.next = new ListNode (2); listA.next.next = new ListNode (3); listA.next.next.next = new ListNode (4); listA.next.next.next.next = new ListNode (5); ListNode listB = new ListNode (6); listB.next = new ListNode (7); listB.next.next = new ListNode (8); listB.next.next.next = listA.next.next.next; System.out.println ( “Linked List A: “); display.List(listA); System.out.println (“Linked List B: “); displayList(listB); int result = findIntersection(listA, listB); System.out.println("Example 1 Output: " + result); //No intersection condition ListNode listC = new ListNode (10); listC.next = new ListNode (20); listC.next.next = new ListNode (30); ListNode listD = new ListNode (40); listD.next = new ListNode (50); listD.next.next = new ListNode (60); int result2 = findIntersection (listC, listD); System.out.println ("Example 2 Output: " + result2); // Output: -1 } } |
| Linked List A: 1->2->3->4->5Linked List B: 6->7->8->4->5 Example 1 Output: 4 Example 2 Output: -1 |
| The Intersection Point Of Two Linked List: Using Hashing |
| import java.util.HashSet; class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class LinkedListIntersection { public static int findIntersection(ListNode headA, ListNode headB) { HashSet<ListNode> nodesSet = new HashSet<>(); ListNode tempA = headA; while (tempA!= null) { nodesSet.add(tempA); tempA = tempA.next; } ListNode tempB = headB; while (tempB!= null) { if (nodesSet.contains(tempB)) { return tempB.val; } tempB = tempB.next; } return -1; } } |
| Linked List A: 1->2->3->4->5Linked List B: 6->7->8->4->5 Example 1 Output: 4 Example 2 Output: -1 |
| The Intersection Point Of Two Linked List: Using Loops |
| class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class LinkedListIntersection { public static int findIntersection(ListNode headA, ListNode headB) { ListNode tempA = headA; ListNode tempB; while (tempA!= null) { tempB = headB; while (tempB!= null) { if (tempA == tempB) { return tempA.val; } tempB = tempB.next; } tempA = tempA.next; } return -1; } |
| Linked List A: 1->2->3->4->5Linked List B: 6->7->8->4->5 Example 1 Output: 4 Example 2 Output: -1 |
| The Intersection Point Of Two Linked Lists: Using Floyd’s Cycle Detection Algorithm |
| public class LinkedListIntersection { private static ListNode findNode (ListNode slow, ListNode list) { int count = 1; for (ListNode pointer = slow; pointer.next! = slow; pointer = pointer.next) { count++; } ListNode current = list; for (int i = 0; i < count; i++) { current = current.next; } while (current!= list) { current = current.next; list = list.next; } return current; } private static ListNode identifyCycle (ListNode list) { ListNode slow = list, fast = list; while (fast!= null && fast.next!= null) { slow = slow.next; fast = fast.next.next; if (slow == fast) { return slow; } } return null; } public static int intersectionPoint(ListNode list1, ListNode list2) { ListNode previous = null, current = list1; while (current != null) { previous = current; current = current.next; } if (previous != null) { previous.next = list1; } ListNode slow = identifyCycle(list2); ListNode intersectionNode = null; if (slow != null) { intersectionNode = findNode(slow, list2); } if (previous != null) { previous.next = null; } int result = intersectionNode == null ? -1 : intersectionNode.val; return result; } } |
| Linked List A: 1->2->3->4->5Linked List B: 6->7->8->4->5 Example 1 Output: 4 Example 2 Output: -1 |