bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 160. Intersection of Two Linked Lists

160. Intersection of Two Linked Lists

Leetcode Solutions

Two Pointers Approach

  1. Initialize two pointers, pA and pB, to the heads of headA and headB respectively.
  2. Traverse the lists with these pointers simultaneously.
  3. When pA reaches the end of list A, redirect it to the head of list B. Do the same for pB when it reaches the end of list B.
  4. If at any point pA and pB meet, return the node as the intersection.
  5. If no intersection is found by the time both pointers have traversed both lists (i.e., both are null), return null.
UML Thumbnail

Hash Table Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...