GATE | GATE CS Mock 2018 | Question 30
Suppose there are two singly linked lists both of which intersect at some point and become a single linked list. The head or start pointers of both the lists are known, but the intersecting node and lengths of lists are not known.
What is worst case time complexity of optimal algorithm to find intersecting node from two intersecting linked lists?
(A) Θ(n*m), where m, n are lengths of given lists
(B) Θ(n^2), where m>n and m, n are lengths of given lists
(C) Θ(m+n), where m, n are lengths of given lists
(D) Θ(min(n, m)), where m, n are lengths of given lists
Explanation: This takes Θ(m+n) time and O(1) space in worst case, where M and N are the total length of the linked lists.
- Traverse the two linked list to find m and n.
- Get back to the heads, then traverse |m − n| nodes on the longer list.
- Now walk in lock step and compare the nodes until you found the common ones.
Option (C) is correct.
Quiz of this Question