Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

Intersection Point of Two Linked Lists in DSA Javascript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Intersection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Find the intersection node value
What is the value of the node where the two linked lists intersect in the following code?
DSA Javascript
class ListNode {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}

// Create intersection node
const intersect = new ListNode(8);
intersect.next = new ListNode(10);

// List A: 3 -> 7 -> 8 -> 10 -> null
const headA = new ListNode(3);
headA.next = new ListNode(7);
headA.next.next = intersect;

// List B: 99 -> 1 -> 8 -> 10 -> null
const headB = new ListNode(99);
headB.next = new ListNode(1);
headB.next.next = intersect;

function getIntersectionNode(headA, headB) {
  let pA = headA;
  let pB = headB;
  while (pA !== pB) {
    pA = pA ? pA.next : headB;
    pB = pB ? pB.next : headA;
  }
  return pA;
}

const intersection = getIntersectionNode(headA, headB);
console.log(intersection ? intersection.val : null);
Anull
B7
C8
D10
Attempts:
2 left
💡 Hint
The intersection node is the first common node shared by both lists.
❓ Predict Output
intermediate
2:00remaining
Output when no intersection exists
What will be printed when the two linked lists do not intersect?
DSA Javascript
class ListNode {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}

// List A: 1 -> 2 -> 3 -> null
const headA = new ListNode(1);
headA.next = new ListNode(2);
headA.next.next = new ListNode(3);

// List B: 4 -> 5 -> 6 -> null
const headB = new ListNode(4);
headB.next = new ListNode(5);
headB.next.next = new ListNode(6);

function getIntersectionNode(headA, headB) {
  let pA = headA;
  let pB = headB;
  while (pA !== pB) {
    pA = pA ? pA.next : headB;
    pB = pB ? pB.next : headA;
  }
  return pA;
}

const intersection = getIntersectionNode(headA, headB);
console.log(intersection ? intersection.val : null);
Anull
B6
C4
D1
Attempts:
2 left
💡 Hint
If no nodes are shared, the function returns null.
🔧 Debug
advanced
2:00remaining
Identify the error in intersection detection
What error does the following code produce when trying to find the intersection point?
DSA Javascript
function getIntersectionNode(headA, headB) {
  let pA = headA;
  let pB = headB;
  while (pA !== pB) {
    pA = pA.next || headB;
    pB = pB.next || headA;
  }
  return pA;
}
ATypeError: Cannot read property 'next' of null
BReturns null correctly
CInfinite loop
DSyntaxError
Attempts:
2 left
💡 Hint
Check what happens when pA or pB is null before accessing .next
❓ Predict Output
advanced
2:00remaining
Intersection node with cycle in one list
What is the output of the code when one list has a cycle and the other intersects at a node inside the cycle?
DSA Javascript
class ListNode {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}

// Create cycle nodes
const cycleNode1 = new ListNode(4);
const cycleNode2 = new ListNode(5);
const cycleNode3 = new ListNode(6);
cycleNode1.next = cycleNode2;
cycleNode2.next = cycleNode3;
cycleNode3.next = cycleNode1; // cycle here

// List A: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> cycle back to 4
const headA = new ListNode(1);
headA.next = new ListNode(2);
headA.next.next = new ListNode(3);
headA.next.next.next = cycleNode1;

// List B: 9 -> 5 -> 6 -> cycle back to 4
const headB = new ListNode(9);
headB.next = cycleNode2;

function getIntersectionNode(headA, headB) {
  let pA = headA;
  let pB = headB;
  let visited = new Set();
  while (pA !== null && !visited.has(pA)) {
    visited.add(pA);
    pA = pA.next;
  }
  while (pB !== null) {
    if (visited.has(pB)) return pB;
    pB = pB.next;
  }
  return null;
}

const intersection = getIntersectionNode(headA, headB);
console.log(intersection ? intersection.val : null);
A4
B5
C9
Dnull
Attempts:
2 left
💡 Hint
The intersection is the first common node visited by both lists.
🧠 Conceptual
expert
2:00remaining
Why does the two-pointer approach find intersection?
Why does the two-pointer approach in the intersection detection algorithm work correctly for lists of different lengths?
ABecause it relies on sorting the nodes by value
BBecause it uses extra memory to store visited nodes
CBecause it only traverses the shorter list twice
DBecause switching pointers to the other list equalizes the path lengths traversed by both pointers
Attempts:
2 left
💡 Hint
Think about how the pointers traverse both lists fully.

Practice

(1/5)
1. What does the term Intersection Point of Two Linked Lists mean?
easy
A. The node where two linked lists merge and share the same next nodes
B. The first node of the longer linked list
C. The last node of the shorter linked list
D. The node with the highest value in both lists

Solution

  1. Step 1: Understand linked list structure

    Linked lists are chains of nodes where each node points to the next node.
  2. Step 2: Define intersection point

    The intersection point is where two lists share the exact same node object, meaning from that node onward, both lists are identical.
  3. Final Answer:

    The node where two linked lists merge and share the same next nodes -> Option A
  4. Quick Check:

    Intersection means shared node = D [OK]
Hint: Intersection means shared node reference, not just value [OK]
Common Mistakes:
  • Thinking intersection means same value, not same node
  • Confusing intersection with concatenation
  • Assuming intersection is always at head
2. Which of the following JavaScript code snippets correctly initializes two linked lists that intersect at node with value 8?
easy
A. const list1 = { val: 3, next: { val: 8, next: null } }; const list2 = { val: 7, next: { val: 8, next: null } };
B. const intersectNode = { val: 8, next: null }; const list1 = { val: 3, next: intersectNode }; const list2 = { val: 7, next: intersectNode };
C. const list1 = { val: 3, next: null }; const list2 = { val: 7, next: null };
D. const intersectNode = { val: 8, next: null }; const list1 = { val: 3, next: { val: 8, next: null } }; const list2 = { val: 7, next: intersectNode };

Solution

  1. Step 1: Check for shared node reference

    const intersectNode = { val: 8, next: null }; const list1 = { val: 3, next: intersectNode }; const list2 = { val: 7, next: intersectNode }; creates one node object for value 8 and both lists point to it, ensuring intersection.
  2. Step 2: Compare other options

    const list1 = { val: 3, next: { val: 8, next: null } }; const list2 = { val: 7, next: { val: 8, next: null } }; creates two separate nodes with value 8, no intersection. const list1 = { val: 3, next: null }; const list2 = { val: 7, next: null }; has no intersection node. const intersectNode = { val: 8, next: null }; const list1 = { val: 3, next: { val: 8, next: null } }; const list2 = { val: 7, next: intersectNode }; has list1 pointing to a new node, not the shared one.
  3. Final Answer:

    shares the same node object for intersection -> Option B
  4. Quick Check:

    Shared node object = A [OK]
Hint: Intersection requires same node object, not just same value [OK]
Common Mistakes:
  • Creating separate nodes with same value instead of shared node
  • Not linking both lists to the intersecting node
  • Confusing node value with node reference
3. Given the following code, what will be the output of console.log(getIntersectionNode(listA, listB)?.val || null);?
function getIntersectionNode(headA, headB) {
  let a = headA, b = headB;
  while (a !== b) {
    a = a ? a.next : headB;
    b = b ? b.next : headA;
  }
  return a;
}

const intersect = { val: 5, next: { val: 6, next: null } };
const listA = { val: 1, next: { val: 2, next: intersect } };
const listB = { val: 9, next: { val: 4, next: intersect } };
medium
A. 5
B. null
C. 6
D. 1

Solution

  1. Step 1: Understand the algorithm

    The function uses two pointers that traverse both lists; when they meet, that node is the intersection.
  2. Step 2: Identify intersection node

    Both lists share the node with value 5, so the function returns that node.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Intersection node value = 5 [OK]
Hint: Two-pointer method returns intersection node or null [OK]
Common Mistakes:
  • Expecting value 6 instead of 5
  • Confusing node value with list head value
  • Not understanding pointer switching logic
4. The following code attempts to find the intersection node of two linked lists but may cause infinite loop when there is no intersection. What is the bug?
function getIntersectionNode(headA, headB) {
  let a = headA, b = headB;
  while (a !== b) {
    a = a.next || headB;
    b = b.next || headA;
  }
  return a;
}
medium
A. The function should return b instead of a
B. The loop condition should be 'while(a && b && a !== b)'
C. The pointers should not switch lists
D. Using 'a.next || headB' incorrectly jumps to headB when a.next is null

Solution

  1. Step 1: Analyze pointer update logic

    Using 'a.next || headB' sets a to headB when a.next is null (while a points to last node), skipping null handling and causing infinite loops when no intersection.
  2. Step 2: Correct pointer switching

    The correct approach is to check if 'a' is null, then switch to headB; otherwise, move to a.next.
  3. Final Answer:

    Using 'a.next || headB' causes skipping headB when a.next is null -> Option D
  4. Quick Check:

    Pointer update must check 'a' null, not 'a.next' [OK]
Hint: Check pointer null before moving to next list head [OK]
Common Mistakes:
  • Using 'a.next || headB' instead of 'a ? a.next : headB'
  • Not handling null pointers correctly
  • Returning wrong pointer at end
5. You have two singly linked lists that intersect. List A length is 7, List B length is 5. How can you find the intersection point efficiently?
hard
A. Reverse both lists and compare from the start
B. Traverse both lists fully and compare node values to find intersection
C. Advance the longer list by difference in lengths, then move both pointers together until they meet
D. Use a hash set to store all values of the shorter list, then check values of the longer list

Solution

  1. Step 1: Calculate length difference

    Find lengths of both lists; difference is 2 (7 - 5).
  2. Step 2: Advance longer list pointer

    Move pointer of longer list ahead by 2 nodes to align both lists equally from intersection point.
  3. Step 3: Move both pointers together

    Traverse both lists simultaneously until pointers are equal, which is the intersection node.
  4. Final Answer:

    Advance longer list by difference in lengths, then move both pointers together until they meet -> Option C
  5. Quick Check:

    Align pointers by length difference = A [OK]
Hint: Align list starts by length difference before simultaneous traversal [OK]
Common Mistakes:
  • Comparing node values instead of node references
  • Not aligning pointers before traversal
  • Using extra space unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard