Good morning! Here's our prompt for today.
Assume you have a Linked List implementation that has the following API:
JAVASCRIPT
1// prepend to start of list
2#prepend(val);
3
4// appends to end of list
5#append(val);
6
7// get an array of node values (for testing)
8#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
and1000000000
- Expected time complexity :
O(n*m)
(the lengths of the two lists) - Expected space complexity :
O(n)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
98
'PASSED: Getting the insection of two lists with node `15` should return a list of `15`.'
function getIntersection(list1, list2) {
// fill in implementation for method
return;
}
​
// Supporting data structures
​
class LinkedList {
constructor() {
this.head = null;
this.tail = null;
}
​
prepend(newVal) {
const currentHead = this.head;
const newNode = new Node(newVal);
newNode.next = currentHead;
this.head = newNode;
​
if (!this.tail) {
this.tail = newNode;
}
}
​
append(newVal) {
const newNode = new Node(newVal);
if (!this.head) {
this.head = newNode;
this.tail = newNode;
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
We'll now take you through what you need to know.
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.