Add Two Numbers β A Clean and Clear Guide


π Problem Overview
The Add Two Numbers problem is a classic linked list problem in technical interviews. It helps you understand how to manipulate linked data structures and deal with digit-by-digit operations β like how we used to add numbers manually on paper.
ποΈ Real-Life Analogy
Letβs say you're working at a small store. A customer pays βΉ273, and you want to calculate change from βΉ956.
Hereβs the breakdown:
βΉ273 β stored as [3 β 7 β 2]
βΉ956 β stored as [6 β 5 β 9]
Add them digit by digit, just like how you do on paper from right to left.
3 β 7 β 2 (273)
+ 6 β 5 β 9 (956)
---------------------
9 β 2 β 2 β 1 (1229)
So the result will be: [9 β 2 β 2 β 1]
π§± Base Concepts You Should Know
Before diving into code, make sure you understand:
What is a Linked List?
A series of connected nodes where each node holds a value and a pointer to the next.How is a Number Stored in Reverse Order?
Number123
becomes[3 β 2 β 1]
.How Carry Works in Addition
When digits add up to more than 9, we carry over the remainder to the next digit.
π§Ό Simple and Clean Python Code
# Definition for singly-linked list node
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
# Core function to add two numbers
def addTwoNumbers(l1, l2):
dummy = ListNode() # placeholder for result
cur = dummy
carry = 0
while l1 or l2 or carry:
total = carry # start with carry from last addition
if l1:
total += l1.val
l1 = l1.next
if l2:
total += l2.val
l2 = l2.next
carry = total // 10 # update carry
dummy.next = ListNode(total % 10) # new digit
dummy = dummy.next
return cur.next # skip dummy node
π Step-by-Step Example
Letβs walk through this:
l1 = [3 β 7 β 2] # 273
l2 = [6 β 5 β 9] # 956
Step-by-step addition:
3 + 6 = 9 β carry = 0 β node = 9
7 + 5 = 12 β carry = 1 β node = 2
2 + 9 + 1 = 12 β carry = 1 β node = 2
β carry = 1 β node = 1 (final carry)
Result Linked List: [9 β 2 β 2 β 1] β 1229
β±οΈ Time & Space Complexity
Operation | Complexity |
Time | O(max(m, n)) where m and n are lengths of the lists |
Space | O(max(m, n)) for the result list |
π§βπ» Interview Tip
π‘ Focus on these points when explaining your solution in an interview:
How you handle carry
Why dummy node helps simplify the logic
Edge cases (e.g., one list is shorter, or carry at the end)
π§© Related Problems to Practice
Add Binary
Multiply Strings
Plus One
Reverse Linked List
π§ Recap
# Final clean solution
def addTwoNumbers(l1, l2):
dummy = ListNode()
cur = dummy
carry = 0
while l1 or l2 or carry:
total = carry
if l1:
total += l1.val
l1 = l1.next
if l2:
total += l2.val
l2 = l2.next
carry = total // 10
dummy.next = ListNode(total % 10)
dummy = dummy.next
return cur.next
β
Clean
β
Understandable
β
Realistic
π Final Thoughts
The Add Two Numbers problem is more than just math β itβs about breaking down problems, handling edge cases, and working with data structures like linked lists.
Take time to visualize the process, write down examples on paper, and step through the code slowly. You'll master it in no time.
Subscribe to my newsletter
Read articles from shaik rasool directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
