Intersection of Two Linked Lists

Solution Code
class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
        if not headA or not headB:
            return None
        
        pA = headA
        pB = headB
        
        while pA != pB:
            pA = pA.next if pA else headB
            pB = pB.next if pB else headA
            
        return pA
Video GuideLeetcode Daily

Time Complexity

O(m + n)

Space Complexity

O(N).