Intersection of linked lists interviewbit solution Python
1. Using HashingThe idea is to traverse the first list and store each node’s address in a hash table. Then traverse the second list and get the address of the first node present in the hash table. This node would be the intersection point. Following is the C++, Java, and Python implementation based on the idea: Show C++
DownloadRun Code Output: Find intersection point of two Linked Lists without finding the length
There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got linked to the second list, forming an inverted Y shaped list. Write a program to get the point where both the linked lists merge. Examples: Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 ^ | 7 -> 8 -> 9 Output: 4 Input: 13 -> 14 -> 5 -> 6 ^ | 10 -> 2 -> 3 -> 4 Output: 14Recommended: Please try your approach on {IDE} first, before moving on to the solution. Prerequisites: Write a function to get the intersection point of two Linked Lists Approach: Take two pointers for the heads of both the linked lists. If one of them reaches the end earlier then use it by moving it to the beginning of the other list. Once both of them go through reassigning they will be equidistance from the collision point. Below is the implementation of the above approach: C++
Java
Python3
C#
Javascript
Output: 15 Article Tags : Linked List two-pointer-algorithm Practice Tags : Linked List two-pointer-algorithm Read Full Article Add two numbers represented by linked lists | Set 1
Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example:
Recommended: Please solve it on “PRACTICE” first, before moving on to the solution. Approach: Traverse both lists to the end and add preceding zeros in the list with lesser digits. Then call a recursive function on the start nodes of both lists which calls itself for the next nodes of both lists till it gets to the end. This function creates a node for the sum of the current digits and returns the carry. The steps are:
Below is the implementation of this approach. C++
C
Java
Python3
C#
Javascript
Output First List is 7 5 9 4 6 Second List is 8 4 Resultant list is 5 0 0 5 6 Complexity Analysis:
Method 2(Using STL): Using the stack data structure Approach :
Code: C++
Output First List : 9 -> 5 -> 0 Second List : 6 -> 7 Sum List : 1 -> 0 -> 1 -> 7 Another Approach with time complexity O(N): The given approach works as following steps:
C++
Output First List : 5 -> 6 -> 3 Second List : 8 -> 4 -> 2 Sum List : 1 -> 4 -> 0 -> 5 Related Article: Add two numbers represented by linked lists | Set 2 Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem. Article Tags : Linked List Accolite Amazon Flipkart MakeMyTrip Microsoft Qualcomm Snapdeal Practice Tags : Flipkart Accolite Amazon Microsoft Snapdeal MakeMyTrip Qualcomm Linked List Read Full Article |