📖 Scenario: Imagine two train tracks that merge into one track at some point. We want to find the exact point where the two tracks join. In programming, this is like finding the intersection point of two linked lists.
🎯 Goal: You will build a program in Go to find the intersection point of two singly linked lists. The program will create two linked lists that share some nodes, then find and print the value at the intersection node.
📋 What You'll Learn
Create two linked lists with some shared nodes
Use pointers to represent nodes and links
Implement a function to find the intersection node
Print the value at the intersection node or -1 if none
💡 Why This Matters
🌍 Real World
Finding intersection points is useful in network routing, version control systems, and merging data streams where two paths converge.
💼 Career
Understanding linked lists and pointer manipulation is essential for software engineers working on low-level data structures, memory management, and performance-critical applications.
Progress0 / 4 steps
1
Create the linked list nodes
Create a struct type called Node with two fields: value int and next *Node. Then create nodes with values 3, 7, 8, 10 where nodes 8 and 10 will be shared later.
DSA Go
Hint
Define the Node struct first. Then create variables for each node using &Node{value: X}.
2
Link the nodes to form two lists with intersection
Link the nodes to form two linked lists: first list is 3 -> 7 -> 8 -> 10, second list is 99 -> 1 -> 8 -> 10. Use the existing nodes for 8 and 10 to create the intersection.
DSA Go
Hint
Link nodes by setting the next field to the next node pointer. Create new nodes for 99 and 1 for the second list.
3
Implement function to find intersection node
Write a function getIntersectionNode that takes two *Node pointers headA and headB and returns the intersection *Node. Use two pointers that traverse both lists and meet at the intersection or nil if none.
DSA Go
Hint
Use two pointers starting at each list head. Move each pointer forward; when it reaches the end, switch to the other list's head. They meet at intersection or nil.
4
Print the intersection node value
Print the value of the intersection node if it exists, otherwise print -1. Use fmt.Println to display the result.
DSA Go
Hint
Check if intersection is not nil. If yes, print intersection.value. Otherwise, print -1.
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
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.
Step 2: Define intersection point
The intersection point is the node where both lists merge and continue as one, sharing the same reference.
Final Answer:
The node where both linked lists share the same reference, meaning they merge. -> Option D
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
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.
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.
Final Answer:
func getIntersectionNode(headA *ListNode, headB *ListNode) *ListNode -> Option C
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
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.
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.
Final Answer:
8 -> Option B
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
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.
Step 2: Identify correct pointer to advance
When diff < 0, advance headB by |diff| steps, else advance headA. The code does the opposite.
Final Answer:
The function incorrectly advances headA when diff < 0 instead of headB. -> Option A
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
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.
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.
Final Answer:
Option A code snippet correctly implements two-pointer technique without extra space. -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.