From 17a5c0f46101d1dbbf03af206229c77b3e7f3213 Mon Sep 17 00:00:00 2001 From: Sanat Kumar Gupta <123228827+SKG24@users.noreply.github.com> Date: Sun, 3 Nov 2024 21:43:52 +0530 Subject: [PATCH 1/2] Added #1031 Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null. --- .../Intersection_of_Two_List/program.py | 54 +++++++++++++++++++ .../Intersection_of_Two_List/readme.md | 27 ++++++++++ 2 files changed, 81 insertions(+) create mode 100644 Algorithms_and_Data_Structures/Linked List/Intersection_of_Two_List/program.py create mode 100644 Algorithms_and_Data_Structures/Linked List/Intersection_of_Two_List/readme.md diff --git a/Algorithms_and_Data_Structures/Linked List/Intersection_of_Two_List/program.py b/Algorithms_and_Data_Structures/Linked List/Intersection_of_Two_List/program.py new file mode 100644 index 0000000000..104b7225ea --- /dev/null +++ b/Algorithms_and_Data_Structures/Linked List/Intersection_of_Two_List/program.py @@ -0,0 +1,54 @@ +class ListNode: + def __init__(self, x): + self.val = x + self.next = None + +class Solution: + def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: + # Using a set to store the nodes of the first linked list + seen_nodes = set() + curr = headA + + while curr: + seen_nodes.add(curr) # Add the current node to the set + curr = curr.next + + curr2 = headB + while curr2: + if curr2 in seen_nodes: # Check if current node is in the set + return curr2 # Intersection found + curr2 = curr2.next + + return None # No intersection + +# Helper function to create a linked list from a list +def create_linked_list(values): + if not values: + return None + head = ListNode(values[0]) + curr = head + for value in values[1:]: + curr.next = ListNode(value) + curr = curr.next + return head + +# Example usage +if __name__ == "__main__": + # Create linked lists for the example + # List A: 1 -> 2 -> 3 + # List B: 4 -> 5 + # Intersection at node with value 3 + intersection_node = ListNode(4) + + headA = create_linked_list([1, 2, 3]) + headA.next.next = intersection_node # Connect intersection + headB = create_linked_list([5, 6, 7, 8]) + headB.next.next = intersection_node # Connect intersection + + solution = Solution() + intersection = solution.getIntersectionNode(headA, headB) + + if intersection: + print(f"Intersection at node with value: {intersection.val}") + else: + print("No intersection") diff --git a/Algorithms_and_Data_Structures/Linked List/Intersection_of_Two_List/readme.md b/Algorithms_and_Data_Structures/Linked List/Intersection_of_Two_List/readme.md new file mode 100644 index 0000000000..1d306ff997 --- /dev/null +++ b/Algorithms_and_Data_Structures/Linked List/Intersection_of_Two_List/readme.md @@ -0,0 +1,27 @@ +# Intersection of Two Linked Lists + +## Problem Statement + +In a linked list, the intersection of two lists occurs when two linked lists share a common node. Given two linked lists, this problem aims to find the node where the two lists intersect. If they do not intersect, the result should be `None`. + +### Example + +Consider the following two linked lists: + +- **List A**: 1 -> 2 -> 3 +- **List B**: 4 -> 5 +- Both lists intersect at the node with value `3`. + +The expected output in this case is: + + +## Approach + +To solve this problem, we can use a hash set to store the nodes of the first linked list. As we traverse the second linked list, we can check if any node exists in the set. If we find a match, that node is the intersection point. + +### Steps: + +1. Traverse the first linked list and add each node to a set. +2. Traverse the second linked list and check if any node is in the set. +3. If a node is found in the set, return that node as the intersection. +4. If no nodes match, return `None`. From c273c2ddacd66c1475d5032d1e3da3193250d463 Mon Sep 17 00:00:00 2001 From: SKG24 Date: Sun, 3 Nov 2024 16:14:48 +0000 Subject: [PATCH 2/2] updating Project-Structure.md --- Project-Structure.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Project-Structure.md b/Project-Structure.md index ba58d4beb2..b6949f2caa 100644 --- a/Project-Structure.md +++ b/Project-Structure.md @@ -97,6 +97,8 @@ * [Program](Algorithms_and_Data_Structures/Linked%20List/Clone_a_List_with_Random_Pointers/program.py) * Detect Cycle In List * [Program](Algorithms_and_Data_Structures/Linked%20List/Detect_Cycle_in_List/program.py) + * Intersection Of Two List + * [Program](Algorithms_and_Data_Structures/Linked%20List/Intersection_of_Two_List/program.py) * [Menu Driven Code For Circular Doubly Linkedlist](Algorithms_and_Data_Structures/Linked%20List/Menu_Driven_Code_for_Circular_Doubly_LinkedList.py) * [Menu Driven Code For Circular Linkedlist](Algorithms_and_Data_Structures/Linked%20List/Menu_Driven_Code_for_Circular_LinkedList.py) * [Menu Driven Code For Doubly Linkedlist](Algorithms_and_Data_Structures/Linked%20List/Menu_Driven_Code_for_Doubly_LinkedList.py)