160. Intersection of Two Linked Lists Leetcode Solution
Intersection of Two Linked Lists Leetcode Problem :
Here we will discuss about the solution of Leetcode Problem Number 160 name Intersection of two linked list using the most optimized approach.
Intersection of Two Linked List Leetcode Solution :
Question :
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.
For example, the following two linked lists begin to intersect at node c1:
The test cases are generated such that there are no cycles anywhere in the entire linked structure.
Note that the linked lists must retain their original structure after the function returns.
Custom Judge:
The inputs to the judge are given as follows (your program is not given these inputs):
- intersectVal – The value of the node where the intersection occurs. This is 0 if there is no intersected node.
- listA – The first linked list.
- listB – The second linked list.
- skipA – The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node.
- skipB – The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node.
The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted.
Constraints :
- The number of nodes of listA is in the m.
- The number of nodes of listB is in the n.
- 1 <= m, n <= 3 * 10^4
- 1 <= Node.val <= 10^5
- 0 <= skipA < m
- 0 <= skipB < n
- intersectVal is 0 if listA and listB do not intersect.
- intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect.
Example 1:
- Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
- Output: Intersected at ‘2’
Example 2:
- Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
- Output: No intersection
Intuition :
The goal is to find the node where two linked lists intersect, if they do. We can approach this problem by storing the nodes of one of the linked lists (let’s say headA) in a data structure that allows quick lookup, like a hash set. Then, we can traverse the other linked list (headB) and check if any of its nodes are in the hash set.
Approach :
- We start by creating an empty hash set called nodeSet.
- We traverse the first linked list, headA, and add each of its nodes to the nodeSet. This allows us to quickly check if a node from headB exists in headA later.
- Next, we traverse the second linked list, headB, and at each step, we check if the current node is in the nodeSet. If we find a node that is in both headA and headB, we have found the intersection point, and we return that node.
- If we reach the end of headB without finding an intersection, we return null to indicate that the two linked lists do not intersect.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Code :
class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ListNode *temp; while(headA != NULL){ temp = headB; while(temp != NULL){ if(headA == temp){ return headA; } temp = temp -> next; } headA = headA -> next; } return NULL; } };
public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { // Create a hash set to store nodes from headA Set< ListNode> nodeSet = new HashSet<>(); // Traverse and store nodes from headA in the hash set ListNode nodeA = headA; while (nodeA != null) { nodeSet.add(nodeA); nodeA = nodeA.next; } // Traverse headB and check if any of its nodes are in the hash set ListNode nodeB = headB; while (nodeB != null) { if (nodeSet.contains(nodeB)) { return nodeB; // Intersection found } nodeB = nodeB.next; } return null; // No intersection found } }
class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: stackA = ['A'] stackB = ['B'] while headA or headB: if headA: stackA.append(headA) headA = headA.next if headB: stackB.append(headB) headB = headB.next prev = None while stackA and stackB: nodeA = stackA.pop(-1) nodeB = stackB.pop(-1) if nodeA != nodeB: return prev prev = nodeA
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment