Find the Intersection Node of Two Linked Lists
Updated
Problem statement
Two singly linked lists are represented by one shared node table. Row nodes[i] describes node identity i as [value, nextIndex], where nextIndex is another node index or -1.
The two lists start at headA and headB. Because both heads use the same node table, reaching the same node index means the lists intersect by identity.
Return the index of the first shared node, or -1 if the lists do not intersect. Do not modify nodes.
Function
findIntersectionNodeIndex(nodes: int[][], headA: int, headB: int) → intExamples
Example 1
nodes = [[4,1],[1,2],[8,3],[4,4],[5,-1],[5,6],[6,2]]headA = 0headB = 5return = 2List A is 0 -> 1 -> 2 -> 3 -> 4, and list B is 5 -> 6 -> 2 -> 3 -> 4. Their first shared node is index 2.
Example 2
nodes = [[1,1],[2,-1],[3,3],[4,-1]]headA = 0headB = 2return = -1The paths 0 -> 1 and 2 -> 3 share no node identity.
Example 3
nodes = [[7,1],[8,2],[9,-1]]headA = 0headB = 1return = 1List B begins inside list A, so its head at index 1 is the first shared node.
Constraints
0 <= nodes.length <= 10^5.- Every row of
nodeshas exactly two integers:[value, nextIndex]. - Each
nextIndexis-1or a valid node index. headAandheadBare-1or valid node indices.- The chains reachable from both heads are acyclic.
-10^9 <= nodes[i][0] <= 10^9.
More Tesla Phone Screen questions
- Flatten a Branched List into a Doubly Linked ListPHONE SCREEN · Seen Sep 2026
- Reduce-Based Map and Filter PipelinePHONE SCREEN · Seen Sep 2026
- Resilient Fake API ClientPHONE SCREEN · Seen Sep 2026
- Merge JSON Array FieldsPHONE SCREEN · Seen Aug 2026
- KV Store with RollbackPHONE SCREEN · Seen Apr 2026
- Multi-Channel Valid ConvolutionPHONE SCREEN · Seen Oct 2024
- Course Schedule FeasibilityPHONE SCREEN · Seen Aug 2024
- Average of a Fixed-Size ArrayPHONE SCREEN · Seen Jun 2024
- Swap Even and Odd BitsPHONE SCREEN · Seen Mar 2023
- Priority, Expiration, and LRU EvictionPHONE SCREEN · Seen Mar 2021
Similar questions at other companies
- Merge Two Sorted ListsLinkedIn · PHONE SCREENLinkedIn interview questions
- Middle of a Linked ListCisco · PHONE SCREENCisco interview questions
- Merge Two Sorted ListsIBM · PHONE SCREENIBM interview questions
- Count Nodes in a ChainApple · PHONE SCREENApple interview questions
- Detect a Cycle in a Linked ListSalesforce · PHONE SCREENSalesforce interview questions
- Find the Intersection Node of Two Linked ListsVisa · PHONE SCREENVisa interview questions
- Reverse a Linked ListMygate · PHONE SCREENMygate interview questions
- Linked List Palindrome in Constant SpaceNVIDIA · PHONE SCREENNVIDIA interview questions
- First Node Two Linked Lists ShareAdobe · PHONE SCREENAdobe interview questions
- Reverse a Singly Linked ListBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
Tesla interview experiences
- Tesla Experienced Infrastructure SWE Interviews: 1 Experience
- Tesla Older SWE Intern Reports: 14 Experiences (Part 4 of 4)
- Tesla Embedded and SRE Intern Loops: 15 Experiences (Part 3 of 4)
- Tesla SWE and Data Intern Rounds: 10 Experiences (Part 2 of 4)
- Tesla Intern Python Interview Prep Guide
- 2017–2026 Tesla Intern OA / Interviews: 83 Experiences (Part 2 :)