Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Find The Intersection Of Two Linked Lists (Main Thread)

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.

Constraints

  • Length of the two linked lists <= 1000
  • The nodes in the list will always contain integer values between -1000000000 and 1000000000
  • Expected time complexity : O(n*m) (the lengths of the two lists)
  • Expected space complexity : O(n)

You can see the full challenge with visuals at this link.

Challenges • Asked over 4 years ago by Denis G.

Jake from AlgoDaily Commented on Oct 16, 2019:

This is the main discussion thread generated for Find The Intersection Of Two Linked Lists.

Denis G. Commented on Oct 16, 2019:

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
}

Jake from AlgoDaily Commented on Nov 30, 2019:

Hi Denis,

Yes, thank you so much for catching that! Fixed.