Skip to content
LC-0160 Easy LeetCode

160. Intersection of Two Linked Lists

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 61% Topics: Hash Table, Linked List, Two Pointers
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(m + n)
# Space: O(1)

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    # @param two ListNodes
    # @return the intersected ListNode
    def getIntersectionNode(self, headA, headB):
        curA, curB = headA, headB
        while curA != curB:
            curA = curA.next if curA else headB
            curB = curB.next if curB else headA
        return curA

Solution from kamyu104/LeetCode-Solutions · MIT