Merging Linked Lists
Problem
You're given two Linked Lists of potentially unequal length. These Linked Lists potentially merge at a shared intersection node. Write a function that returns the intersection node or returns None / null if there is no intersection.
Each LinkedList node has an integer value as well as a next node pointing to the next node in the list or to None null if it's the tail of the list.
Note: Your function should return an existing node. It should not modify either Linked List, and it should not create any new Linked Lists.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Sample Output
Solutions
Official
Solution 1
- choose one of the two lists to iterate through and create a set of all of the nodes
- iterate through the other list, check to see if any of the nodes are in the first list
Solution 2
- get the length of two lists
- iterate the bigger list first to get two lists to the same length.
- iterate over two lists,then it will reach the intersection point at the exact same time.
Solution 3
- having both pointers go through both lists.
Everyone's
Iterate over the same list repeatedly
月
Hao
Solution3: iterate through another list
東
YC
Supplement / Discussion