↓Skip to main content
  1. LeetCode/

LeetCode 160: Intersection of Two Linked Lists

·2 mins· ·
LeetCode Easy Hash-Table Linked-List Two-Pointers
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-20

  • Submissions: Runtime Error ×2 → Wrong Answer → Time Limit Exceeded → Runtime Error ×7 → Accepted ×2 → Wrong Answer ×2 → Accepted ×2 → Runtime Error → Time Limit Exceeded → Accepted ×2

Approach
#

After the intersection the lists share nodes. Pointer a walks A then B, and b walks B then A (Linked List Template 6). Both cover lenA + lenB steps, so they meet at the intersection; with none they reach None together and the loop ends.

My Attempts
#

This took 21 submissions in 2022:

  • I first stored A’s nodes in a hash table; attempt 1 called append on a dict (RE); after switching to a set, the loop over B forgot to advance B (TLE), and it returned True / False instead of a node.
  • After switching to head swapping, one version wrote B = B.next if B else B: when B finished it switched to itself (None) and never walked A, so with A [1, 3] and B [3] (sharing [3]) it returned None.

Solution
#

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
        a, b = headA, headB
        while a is not b:
            a = a.next if a else headB
            b = b.next if b else headA
        return a

Complexity
#

Time O(m + n), space O(1).

Mistakes to Avoid
#

  • Compare the nodes themselves (is), not values.
  • After finishing your own list, switch to the other list’s head, not your own.

Interview Explanation
#

Each pointer walks its own list and then switches to the other’s head; both walk lenA + lenB steps, so they arrive at the intersection together, or both become None. Time O(m + n), space O(1).

Related

LeetCode 141: Linked List Cycle
·2 mins
LeetCode Easy Hash-Table Linked-List Two-Pointers Floyds-Cycle-Finding-Algorithm
LeetCode 141 note: Floyd’s cycle detection, fast two steps and slow one; with a cycle they must meet, each advancing from its own position
LeetCode 169: Majority Element
·2 mins
LeetCode Easy Array Hash-Table Divide-and-Conquer Sorting
LeetCode 169 note: Boyer-Moore voting, where different values cancel in pairs and the survivor is the one appearing more than half the time
LeetCode 125: Valid Palindrome
·2 mins
LeetCode Easy Two-Pointers String
LeetCode 125 note: two pointers skip non-alphanumeric characters and compare lowercase letters, with digits included
LeetCode 83: Remove Duplicates from Sorted List
·2 mins
LeetCode Easy Linked-List
LeetCode 83 note: sorted, so duplicates are adjacent; skip cur.next while it equals cur, and advance only otherwise
LeetCode 27: Remove Element
·1 min
LeetCode Easy Array Two-Pointers
LeetCode 27 note: the write pointer copies only values not equal to val, returning how many were written
LeetCode 28: Find the Index of the First Occurrence in a String
·1 min
LeetCode Easy Two-Pointers String String-Matching Z-Algorithm
LeetCode 28 note: try matching needle at every start, or use str.find; index raises when not found