Add Two Numbers Represented as Linked List in DSA Go - Time & Space Complexity
We want to understand how the time needed to add two numbers stored as linked lists grows as the numbers get longer.
How does the work change when the linked lists have more digits?
Analyze the time complexity of the following code snippet.
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
dummy := &ListNode{}
current := dummy
carry := 0
for l1 != nil || l2 != nil || carry != 0 {
sum := carry
if l1 != nil {
sum += l1.Val
l1 = l1.Next
}
if l2 != nil {
sum += l2.Val
l2 = l2.Next
}
carry = sum / 10
current.Next = &ListNode{Val: sum % 10}
current = current.Next
}
return dummy.Next
}
This code adds two numbers digit by digit, stored in linked lists, and returns the sum as a new linked list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The single loop that moves through both linked lists simultaneously.
- How many times: It runs once for each digit in the longer linked list, plus one extra if there is a carry at the end.
As the number of digits (nodes) increases, the loop runs more times, roughly once per digit.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps through the loop |
| 100 | About 100 steps through the loop |
| 1000 | About 1000 steps through the loop |
Pattern observation: The work grows directly with the number of digits; doubling digits roughly doubles the work.
Time Complexity: O(n)
This means the time to add the numbers grows linearly with the number of digits in the longer list.
[X] Wrong: "The time depends on both lists multiplied together, like O(n * m)."
[OK] Correct: The code only moves through each list once, so the time depends on the longer list length, not the product of lengths.
Understanding this linear time complexity helps you explain how your code handles numbers of any size efficiently, a skill valued in many coding challenges.
"What if the digits were stored in forward order instead of reverse? How would the time complexity change?"
Practice
Solution
Step 1: Understand the problem statement and identify the expected output
The problem requires adding two numbers where each number is represented by a linked list, with each node holding a single digit. The sum should also be returned as a linked list, where each node contains a digit of the result.Final Answer:
Add two numbers represented as linked lists and return their sum as a linked list -> Option CQuick Check:
Adding digits node by node = Add two numbers where each digit is stored in a node of a linked list, and return the sum as a linked list. [OK]
- Confusing addition with multiplication
- Ignoring carry over between digits
- Returning a number instead of a linked list
Solution
Step 1: Check struct field types and verify pointer usage
The node should hold an integer value and a pointer to the next node, so Val is int and Next is *ListNode. Next must be a pointer to ListNode to allow linking nodes dynamically.Final Answer:
type ListNode struct { Val int; Next *ListNode } -> Option AQuick Check:
Pointer to next node = type ListNode struct { Val int; Next *ListNode } [OK]
- Using value type instead of pointer for Next
- Using wrong field types like string for digits
- Missing semicolons or commas in struct
Solution
Step 1: Add digits from head nodes with carry and form the result linked list
2 + 5 = 7, carry 0; next digits 4 + 6 = 10, store 0 carry 1; next digits 3 + 4 + 1 = 8. The result digits are 7 -> 0 -> 8 representing 807.Final Answer:
7 -> 0 -> 8 -> null -> Option AQuick Check:
342 + 465 = 807 = 7->0->8 [OK]
- Not carrying over when sum >= 10
- Adding digits in wrong order
- Confusing node values with actual number order
Solution
Step 1: Analyze loop condition and identify missing processing
The loop runs only while both l1 and l2 are not nil, so it stops if one list is longer. Remaining nodes in the longer list and any carry after loop are not handled, causing incorrect results.Final Answer:
The loop stops when either list ends, missing remaining nodes in longer list -> Option DQuick Check:
Loop condition misses longer list nodes = The loop stops when either list ends, missing remaining nodes in longer list. [OK]
- Using AND instead of OR in loop condition
- Ignoring leftover carry after loop
- Not linking remaining nodes after loop
Solution
Step 1: Add digits with carry step-by-step and add final carry as new node
9+1=10, store 0 carry 1; next 9+0+1=10, store 0 carry 1; repeat for all 9s, each time store 0 carry 1. After last 9, carry is 1, so add a new node with value 1 at the end.Final Answer:
0 -> 0 -> 0 -> 0 -> 1 -> null -> Option BQuick Check:
9999 + 1 = 10000 = 0->0->0->0->1 [OK]
- Not adding extra node for leftover carry
- Stopping addition too early
- Misplacing the carry node in the list
Ready to prove it?
Pass the test and this pattern moves unknown
