160. Intersection of Two Linked Lists¶
Edited by Charles
Easy · Python · LinkedList
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Fast and slow pointers do not work in this scenario. Assume two pointers set off respectively at the headA and headB. When they reach None in the end, redirect them to the other list.
Original study notes follow, including intermediate snippets and earlier attempts.
- Fast and slow pointers do not work in this scenario.
- Assume two pointers set off respectively at the headA and headB. When they reach None in the end, redirect them to the other list.
- Thus, if two lists meet up at a certain node, two pointers will equal to each other on the second iteration.
- If two lists do not meet up at a certain node, both of them finally will become None.
- Both scenarios break the while loop (p1 ≠ p2).
- Note: the reason why p1 and p2 are checked in if condition instead of p1.next and p2.next is that considering two lists that do not meet up, we need to allow p1 and p2 to be None, or there will be infinite loop, since the only place for two pointers equal to each other is None.
- See the logic in below graph.

Study record
| Field | Value |
|---|---|
| Attempts | 2 |
| Mastery | ❚❚ |
| The Latest Attempt | January 5, 2026 |
| Feedback | This question is quite tricky! Strongly recommended for multiple attempts! |
Python solution¶
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
p1, p2 = headA, headB
while p1 != p2:
if not p1:
p1 = headB
else:
p1 = p1.next
if not p2:
p2 = headA
else:
p2 = p2.next
return p1