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


Answer: (C)

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.

  1. Traverse the two linked list to find m and n.
  2. Get back to the heads, then traverse |m − n| nodes on the longer list.
  3. Now walk in lock step and compare the nodes until you found the common ones.

Option (C) is correct.

Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.