Tensorum
YouTubeसीखना शुरू करें
LeetCode 160 · Intersection

Intersection of Two Linked Lists

LeetCode 160 · आसान · समाधान, intuition और कदम-दर-कदम वॉकथ्रू

Switch heads at the end; equal path lengths make them meet.

आसानHash TableLinked ListTwo Pointers

मैंने Intersection of Two Linked Lists (LeetCode 160) ऐसे हल किया — Intersection पैटर्न का एक आसान प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।

नीचे का विवरण अंग्रेज़ी में है।

Intuition

Walk pointer A through list A then list B, and pointer B through list B then list A. Both travel the same total distance, so they line up and meet at the intersection — or both reach the end together if there isn't one.

Approach

  1. 1

    p1 = headA, p2 = headB.

  2. 2

    Step both; when one reaches the end, jump to the other list's head.

  3. 3

    Stop when they're the same node (or both None).

मैंने कैसे हल किया

Two-pointer head switchingPythonO(m+n) time · O(1) space
class Solution:
    def getIntersectionNode(self, headA, headB):
        p1 = headA
        p2 = headB
        while p1 != p2:
            p1 = headB if p1 is None else p1.next
            p2 = headA if p2 is None else p2.next
        return p1
स्टेप-थ्रू लोड हो रहा है…

Intersection of Two Linked Lists — अक्सर पूछे जाने वाले प्रश्न

Intersection of Two Linked Lists के पीछे intuition क्या है?

Walk pointer A through list A then list B, and pointer B through list B then list A. Both travel the same total distance, so they line up and meet at the intersection — or both reach the end together if there isn't one.

Intersection of Two Linked Lists (LeetCode 160) कैसे हल करें?

1. p1 = headA, p2 = headB. 2. Step both; when one reaches the end, jump to the other list's head. 3. Stop when they're the same node (or both None).

Intersection of Two Linked Lists की time और space complexity क्या है?

Time: O(m + n)। Space: O(1)। (Accepted approach: Two-pointer head switching।)

Intersection of Two Linked Lists में आम गलतियाँ क्या हैं?

Compare node identity, not values.