Bird
Raised Fist0
DSA Goprogramming~10 mins

Intersection Point of Two Linked Lists in DSA Go - Execution Trace

Choose your learning style10 modes available
Concept Flow - Intersection Point of Two Linked Lists
Start at head of List A and List B
↓
Traverse both lists simultaneously
↓
If pointer reaches end, switch to other list's head
↓
Check if pointers are equal (intersection)
Yes No↓
Return intersection
↓
Done
Start pointers at heads of both lists, move forward. When a pointer reaches the end, switch it to the other list's head. Continue until pointers meet or both reach end (no intersection).
Execution Sample
DSA Go
func getIntersectionNode(headA, headB *ListNode) *ListNode {
    a, b := headA, headB
    for a != b {
        if a == nil {
            a = headB
        } else {
            a = a.Next
        }
        if b == nil {
            b = headA
        } else {
            b = b.Next
        }
    }
    return a
}
Two pointers traverse both lists; when reaching end, switch to other list head. They meet at intersection or nil.
Execution Table
StepOperationPointer APointer BVisual StateNotes
1StartNode A1Node B1List A: A1->A2->C1->C2->C3 List B: B1->B2->B3->C1->C2->C3Pointers at heads
2Move forwardNode A2Node B2Pointers move to next nodesNo intersection yet
3Move forwardNode C1Node B3Pointers at C1 and B3No intersection yet
4Move forwardNode C2Node C1Pointers at C2 and C1No intersection yet
5Move forwardNode C3Node C2Pointers at C3 and C2No intersection yet
6Move forwardnilNode C3Pointer A reached end, pointer B at C3Switch pointer A to headB next step
7Switch pointer A to headB, move pointer B forwardNode B1nilPointer B reached endSwitch pointer B to headA next step
8Move forwardNode B2Node A1Pointers at B2 and A1No intersection yet
9Move forwardNode B3Node A2Pointers at B3 and A2No intersection yet
10Move forwardNode C1Node C1Pointers meet at C1Intersection found
11Return intersectionNode C1Node C1Intersection node C1End traversal
💡 Pointers meet at Node C1, intersection found
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8After Step 10Final
Pointer ANode A1Node A2Node C2nilNode B2Node C1Node C1
Pointer BNode B1Node B2Node C1Node C3Node A1Node C1Node C1
Key Moments - 3 Insights
Why do we switch pointer A to headB when it reaches the end of list A?
Switching pointer A to headB ensures it traverses list B after finishing list A, aligning lengths so both pointers meet at intersection or end. See execution_table step 6 and 7.
What happens if there is no intersection between the two lists?
Both pointers will eventually become nil after traversing both lists fully without meeting. The loop ends when a == b == nil, meaning no intersection. This is implied after step 11 if no intersection found.
Why do pointers meet at the intersection node and not before?
Because after switching heads, pointers traverse equal total lengths. They align at the intersection node, as shown in execution_table step 10 where both pointers point to Node C1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what node is Pointer A at after step 4?
ANode A2
BNode B3
CNode C2
DNode C1
💡 Hint
Check the 'Pointer A' column in execution_table row for step 4.
At which step do both pointers first point to the same node?
AStep 6
BStep 10
CStep 8
DStep 3
💡 Hint
Look for the first row where 'Pointer A' and 'Pointer B' columns have the same node.
If list B was longer by one node before intersection, how would the pointer switching change?
APointer B would switch to headA later
BPointer A would switch to headB later
CPointers would meet earlier
DPointers would never meet
💡 Hint
Consider how pointer B traverses list B fully before switching to list A.
Concept Snapshot
Intersection Point of Two Linked Lists:
- Use two pointers starting at heads of each list.
- Move pointers forward one node at a time.
- When a pointer reaches end, switch it to the other list's head.
- Continue until pointers meet (intersection) or both are nil (no intersection).
- This aligns traversal lengths to find intersection efficiently.
Full Transcript
To find the intersection point of two linked lists, we use two pointers starting at the heads of each list. We move both pointers forward one node at a time. When a pointer reaches the end of its list, we switch it to the head of the other list. This way, both pointers traverse the combined lengths of the two lists. Eventually, if there is an intersection, the pointers will meet at the intersection node. If not, both pointers will become nil, indicating no intersection. This method ensures both pointers travel equal distances, aligning them to detect the intersection efficiently.

Practice

(1/5)
1. What does the term intersection point mean in the context of two singly linked lists?
easy
A. The last node of the shorter linked list.
B. The node where the values of both lists are equal but references differ.
C. The first node of the longer linked list.
D. The node where both linked lists share the same reference, meaning they merge.

Solution

  1. Step 1: Understand linked list nodes and references

    Each node in a linked list has a reference (pointer) to the next node. Two lists intersect if they share the same node reference at some point.
  2. Step 2: Define intersection point

    The intersection point is the node where both lists merge and continue as one, sharing the same reference.
  3. Final Answer:

    The node where both linked lists share the same reference, meaning they merge. -> Option D
  4. Quick Check:

    Intersection means shared node reference [OK]
Hint: Intersection means same node reference, not just same value [OK]
Common Mistakes:
  • Confusing same value with same reference
  • Thinking intersection is at the start of lists
  • Assuming intersection means equal length
2. Which of the following Go function signatures correctly represents a function to find the intersection point of two singly linked lists?
easy
A. func getIntersectionNode(headA ListNode, headB *ListNode) *ListNode
B. func getIntersectionNode(headA ListNode, headB ListNode) ListNode
C. func getIntersectionNode(headA *ListNode, headB *ListNode) *ListNode
D. func getIntersectionNode(headA *ListNode, headB *ListNode) int

Solution

  1. Step 1: Understand Go pointer usage for linked lists

    Linked list nodes are usually handled via pointers to allow traversal and modification. So parameters should be pointers.
  2. Step 2: Check return type for intersection node

    The function should return a pointer to the intersecting node or nil if none, so return type is *ListNode.
  3. Final Answer:

    func getIntersectionNode(headA *ListNode, headB *ListNode) *ListNode -> Option C
  4. Quick Check:

    Use pointers for nodes and return pointer [OK]
Hint: Use pointers for linked list nodes in Go function signatures [OK]
Common Mistakes:
  • Passing nodes by value instead of pointer
  • Returning wrong type like int instead of node pointer
  • Mixing pointer and non-pointer parameters
3. Given the following Go code snippet, what will be printed?
type ListNode struct {
    Val  int
    Next *ListNode
}

func main() {
    common := &ListNode{Val: 8, Next: &ListNode{Val: 10, Next: nil}}
    listA := &ListNode{Val: 3, Next: &ListNode{Val: 7, Next: common}}
    listB := &ListNode{Val: 99, Next: &ListNode{Val: 1, Next: common}}

    intersection := getIntersectionNode(listA, listB)
    if intersection != nil {
        fmt.Println(intersection.Val)
    } else {
        fmt.Println(-1)
    }
}

func getIntersectionNode(headA, headB *ListNode) *ListNode {
    nodes := make(map[*ListNode]bool)
    for headA != nil {
        nodes[headA] = true
        headA = headA.Next
    }
    for headB != nil {
        if nodes[headB] {
            return headB
        }
        headB = headB.Next
    }
    return nil
}
medium
A. 10
B. 8
C. -1
D. 3

Solution

  1. Step 1: Understand the linked lists and common node

    listA: 3 -> 7 -> 8 -> 10 listB: 99 -> 1 -> 8 -> 10 The common node starts at value 8.
  2. Step 2: Trace getIntersectionNode function

    The function stores all nodes of listA in a map. Then it checks nodes of listB. The first node in listB found in map is the intersection: node with value 8.
  3. Final Answer:

    8 -> Option B
  4. Quick Check:

    First shared node value = 8 [OK]
Hint: Map nodes of first list, check second list for first match [OK]
Common Mistakes:
  • Printing value of last node instead of intersection
  • Confusing node value with node reference
  • Returning nil when intersection exists
4. The following Go function is intended to find the intersection point of two linked lists but has a bug. What is the bug?
func getIntersectionNode(headA, headB *ListNode) *ListNode {
    lengthA, lengthB := 0, 0
    currA, currB := headA, headB

    for currA != nil {
        lengthA++
        currA = currA.Next
    }
    for currB != nil {
        lengthB++
        currB = currB.Next
    }

    diff := lengthA - lengthB
    if diff < 0 {
        diff = -diff
        for i := 0; i < diff; i++ {
            headA = headA.Next
        }
    } else {
        for i := 0; i < diff; i++ {
            headB = headB.Next
        }
    }

    for headA != nil && headB != nil {
        if headA == headB {
            return headA
        }
        headA = headA.Next
        headB = headB.Next
    }
    return nil
}
medium
A. The function incorrectly advances headA when diff < 0 instead of headB.
B. The function does not handle the case when one list is empty.
C. The pointers headA and headB are advanced incorrectly when diff < 0.
D. The diff calculation should be absolute value before advancing pointers.

Solution

  1. Step 1: Analyze pointer advancement logic

    If lengthA < lengthB (diff < 0), the shorter list is A, so we should advance headB to align lengths, but code advances headA incorrectly.
  2. Step 2: Identify correct pointer to advance

    When diff < 0, advance headB by |diff| steps, else advance headA. The code does the opposite.
  3. Final Answer:

    The function incorrectly advances headA when diff < 0 instead of headB. -> Option A
  4. Quick Check:

    Advance pointer of longer list, not shorter [OK]
Hint: Advance pointer of longer list by difference in length [OK]
Common Mistakes:
  • Advancing pointer of shorter list instead of longer
  • Not taking absolute value of length difference
  • Ignoring empty list cases
5. You have two singly linked lists that may or may not intersect. Write a Go function that returns the intersection node using the two-pointer technique without extra space. Which of the following code snippets correctly implements this approach?
hard
A. func getIntersectionNode(headA, headB *ListNode) *ListNode { if headA == nil || headB == nil { return nil } pA, pB := headA, headB for pA != pB { if pA == nil { pA = headB } else { pA = pA.Next } if pB == nil { pB = headA } else { pB = pB.Next } } return pA }
B. func getIntersectionNode(headA, headB *ListNode) *ListNode { visited := make(map[*ListNode]bool) for headA != nil { visited[headA] = true headA = headA.Next } for headB != nil { if visited[headB] { return headB } headB = headB.Next } return nil }
C. func getIntersectionNode(headA, headB *ListNode) *ListNode { lengthA, lengthB := 0, 0 currA, currB := headA, headB for currA != nil { lengthA++ currA = currA.Next } for currB != nil { lengthB++ currB = currB.Next } diff := lengthA - lengthB if diff < 0 { diff = -diff for i := 0; i < diff; i++ { headA = headA.Next } } else { for i := 0; i < diff; i++ { headB = headB.Next } } for headA != nil && headB != nil { if headA == headB { return headA } headA = headA.Next headB = headB.Next } return nil }
D. func getIntersectionNode(headA, headB *ListNode) *ListNode { for headA != nil { for headB != nil { if headA == headB { return headA } headB = headB.Next } headA = headA.Next } return nil }

Solution

  1. Step 1: Understand two-pointer technique

    Two pointers start at heads of each list. When a pointer reaches end, it switches to the other list's head. They meet at intersection or nil.
  2. Step 2: Analyze code snippets

    func getIntersectionNode(headA, headB *ListNode) *ListNode { if headA == nil || headB == nil { return nil } pA, pB := headA, headB for pA != pB { if pA == nil { pA = headB } else { pA = pA.Next } if pB == nil { pB = headA } else { pB = pB.Next } } return pA } correctly implements this logic with pointer switching and loop until pointers meet. func getIntersectionNode(headA, headB *ListNode) *ListNode { visited := make(map[*ListNode]bool) for headA != nil { visited[headA] = true headA = headA.Next } for headB != nil { if visited[headB] { return headB } headB = headB.Next } return nil } uses extra space (map). func getIntersectionNode(headA, headB *ListNode) *ListNode { lengthA, lengthB := 0, 0 currA, currB := headA, headB for currA != nil { lengthA++ currA = currA.Next } for currB != nil { lengthB++ currB = currB.Next } diff := lengthA - lengthB if diff < 0 { diff = -diff for i := 0; i < diff; i++ { headA = headA.Next } } else { for i := 0; i < diff; i++ { headB = headB.Next } } for headA != nil && headB != nil { if headA == headB { return headA } headA = headA.Next headB = headB.Next } return nil } uses length difference method. func getIntersectionNode(headA, headB *ListNode) *ListNode { for headA != nil { for headB != nil { if headA == headB { return headA } headB = headB.Next } headA = headA.Next } return nil } is inefficient nested loops.
  3. Final Answer:

    Option A code snippet correctly implements two-pointer technique without extra space. -> Option A
  4. Quick Check:

    Two-pointer switch method = func getIntersectionNode(headA, headB *ListNode) *ListNode { if headA == nil || headB == nil { return nil } pA, pB := headA, headB for pA != pB { if pA == nil { pA = headB } else { pA = pA.Next } if pB == nil { pB = headA } else { pB = pB.Next } } return pA } [OK]
Hint: Switch pointers to other list head when reaching end [OK]
Common Mistakes:
  • Using extra space instead of two-pointer method
  • Not switching pointers after reaching list end
  • Using nested loops causing O(n^2) time
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard