Intersection Point of Two Linked Lists in DSA Rust - Build from Scratch
ListNode with a field val of type i32 and a field next of type Option. Then create two linked lists named list1 and list2 that intersect at a node with value 8. The first list should be 4 -> 1 -> 8 -> 4 -> 5, and the second list should be 5 -> 6 -> 1 -> 8 -> 4 -> 5, sharing the nodes starting from the node with value 8.Start by defining the ListNode struct. Then create the shared tail nodes first. Use Option for the next pointer. Finally, build list1 and list2 by linking to the shared nodes.
length that takes a reference to an Option<Box<ListNode>> and returns the length as usize. Then create two variables len1 and len2 that store the lengths of list1 and list2 respectively by calling the length function.Write a function length that uses a loop to count nodes until it reaches None. Then call this function for both lists and store the results in len1 and len2.
get_intersection_node that takes two references to Option<Box<ListNode>> named headA and headB. Use the lengths len1 and len2 to advance the longer list's pointer so both lists have equal remaining length. Then move both pointers step-by-step until they point to the same node (by pointer equality). Return an Option<&ListNode> which is the intersection node or None if no intersection exists. Call this function with list1 and list2 and store the result in intersection.Use pointer equality with std::ptr::eq to check if two nodes are the same. Advance the longer list's pointer by the difference in lengths. Then move both pointers together until they meet or reach the end.
match statement on the variable intersection. If it is Some(node), print "Intersection at node with value: {node.val}". If it is None, print "No intersection found".Use a match on intersection. If Some(node), print the node's value. Otherwise, print that no intersection was found.
Practice
intersection point mean in the context of two linked lists?Solution
Step 1: Understand linked list nodes
Each node in a linked list has a value and a reference (memory address) to the next node.Step 2: Define intersection point
The intersection point is where two linked lists share the exact same node reference, meaning they merge at that node.Final Answer:
The node where both linked lists share the same reference (memory address). -> Option CQuick Check:
Intersection means shared node reference [OK]
- Confusing same value with same node reference
- Thinking intersection is about list length
- Assuming intersection is always at the start
Solution
Step 1: Understand Rust linked list node
Rust needs Option<Box<Node>> for next to allow None and heap allocation.Step 2: Check each option
A uses raw Node causing infinite size error. C uses reference without lifetime. D lacks Option so cannot represent end of list. The correct declaration uses Option<Box<Node>>.Final Answer:
struct Node { value: i32, next: Option<Box<Node>> } -> Option AQuick Check:
Use Option<Box<Node>> for next pointer [OK]
- Omitting Option causing infinite size error
- Using references without lifetimes
- Using raw Node type causing recursive size
Solution
Step 1: Identify shared nodes
Lists share nodes with values 4 and 5, meaning intersection starts at node with value 4.Step 2: Determine intersection point
The first shared node is the intersection point, which is value 4.Final Answer:
4 -> Option AQuick Check:
First shared node value = 4 [OK]
- Choosing last shared node value
- Picking non-shared node values
- Confusing node value with node reference
fn get_intersection_node(head_a: Option<&Box<Node>>, head_b: Option<&Box<Node>>) -> Option<&Box<Node>> {
let mut a = head_a;
let mut b = head_b;
while a != b {
a = if let Some(node) = a { node.next.as_ref() } else { head_b };
b = if let Some(node) = b { node.next.as_ref() } else { head_a };
}
a
}
What is the main bug in this code?Solution
Step 1: Analyze compilation
The function uses bare references (&Box<Node>) in Option without lifetime parameters.Step 2: Rust lifetime rules
Explicit lifetime annotations are required (e.g., fn<'a>(head_a: Option<&'a Box<Node>>, ...) -> Option<&'a Box<Node>>), otherwise it fails to compile due to unresolved lifetimes.Final Answer:
It uses references without proper lifetime annotations causing compile error. -> Option DQuick Check:
Missing lifetime specifiers [OK]
- Assuming != compares pointers correctly
- Ignoring infinite loop possibility
- Overlooking lifetime errors
Solution
Step 1: Understand length difference method
Calculate difference in lengths (7 - 5 = 2), advance longer list by 2 nodes to align both lists.Step 2: Move both pointers together
Then move both pointers one step at a time until they point to the same node (intersection point).Final Answer:
Calculate length difference, advance longer list by difference, then move both pointers together. -> Option BQuick Check:
Align lists by length difference for efficient intersection [OK]
- Using extra memory unnecessarily
- Reversing lists which is inefficient
- Comparing node values instead of references
Ready to prove it?
Pass the test and this pattern moves unknown
