Bloomberg Interview Question

How can you efficiently detect whether two linked lists converged? If they do, how do you identify at which node this occurs?

Interview Answers

Anonymous

Aug 14, 2014

The two single linked lists share the same suffix...but do not share the same prefix? Reverse both lists (dequeue, then enqueue onto a new head). Compare heads of both (reversed) lists until there is a mismatch.

2

Anonymous

Apr 3, 2015

This is from geeks for geeks. Let length of first list be l1 and of second list be l2 (u need to traverse both the lists to find the length !) Take diff = Math.ab(l1-l2). Traverse the longer list till diff. From here on check each node (check the next pointer of each node)

1

Anonymous

Oct 5, 2014

use a hash table, when visiting the first list then for the second, if encounter a same value in hash table, the intersection is found.

Anonymous

Oct 21, 2014

Starting from the head of two list, directly compare the address value of pointer of the nodes

Anonymous

Nov 18, 2014

Just Check the whether the pointer to the last node is the same