Here is the interview question prompt, presented for reference.
Assume you have a Linked List implementation that has the following API:
// prepend to start of list
#prepend(val);
// appends to end of list
#append(val);
// get an array of node values (for testing)
#toArray();
Can you write a method getIntersection(list1, list2)
to find the intersection of two linked lists?
The return value should be a new linked list.
1000
-1000000000
and 1000000000
O(n*m)
(the lengths of the two lists)O(n)
You can see the full challenge with visuals at this link.
Challenges • Asked about 5 years ago by Denis G.
This is the main discussion thread generated for Find The Intersection Of Two Linked Lists.
There is a bug in the list implementation.
in append(newVal)
[...]
if(!this.head) {
this.head = newNode;
this.head = newNode; // Should be this.tail = newNode
}
Hi Denis,
Yes, thank you so much for catching that! Fixed.