Hey everyone! I’m Mahdi Shamlou, and I’m continuing my new series on classic LeetCode problems. After the legendary Two Sum, let’s tackle the next one: Problem #2 — Add Two Numbers.

You can visit my LeetCode repo in my github 👇
https://github.com/mahdi0shamlou/LeetCode
This is a Medium difficulty problem, but it’s an absolute classic — it shows up a lot in coding interviews at big tech companies. Solving it well proves you’re comfortable with linked lists, pointer manipulation, and handling carry like in elementary school addition.
Problem Statement: Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each node contains a single digit.
Add the two numbers and return the sum as a linked list (also in reverse order).
Examples
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807
Input: l1 = [0], l2 = [0]
Output: [0]
-------------------------
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,9,9,9,1]
Explanation: 9999999 + 9999 = 10009998
A Cool & Unique Approach: Recursive Solution
The most common way is iterative (using a dummy node and carry), which is super efficient and safe. But today, I want to share a more interesting and elegant recursive approach that I came up with — it’s less common, feels very functional, and beautifully mirrors how we think about addition digit-by-digit while propagating the carry.
The idea is simple:
We create a helper recursive function that takes the current nodes from both lists and the carry from the previous digit.
Add the values + carry, compute the new digit (% 10) and new carry.
Recursively compute the next node.
Handle cases where one list is longer by passing a dummy node with value 0.
Base case: when both lists end and no carry left, return None.
Here’s my Python solution:
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution(object):
def addTwoNumbers(self, l1, l2):
return self.sum(l1, l2, 0)
def sum(self, l1, l2, added):
posetive = l1.val + l2.val + added
added = 0
if posetive >= 10:
posetive = posetive - 10
added = 1
if l1.next != None and l2.next != None:
list_node = ListNode(val=posetive, next=self.sum(l1.next, l2.next, added))
elif l2.next == None and l1.next != None:
list_node = ListNode(val=posetive, next=self.sum(l1.next, ListNode(val=0, next=None), added))
elif l2.next != None and l1.next == None:
list_node = ListNode(val=posetive, next=self.sum(ListNode(val=0, next=None), l2.next, added))
else:
if added:
list_node = ListNode(val=posetive, next=ListNode(val=added))
else:
list_node = ListNode(val=posetive, next=None)
return list_node
Why this recursive approach is interesting:
Elegant and intuitive — recursion naturally handles the “go to next digit” flow.
Clean structure — no dummy nodes or while loops; the recursion tree builds the result bottom-up.
Performance — O(max(n, m)) time and space, beats many submissions.
That said, for very long lists (up to 100 nodes), Python’s recursion depth might be a concern, so iterative is safer in production. But this recursive version is super fun and impressive in interviews if you explain the edge cases well!
If you prefer the standard iterative way or know other tricks, share in the comments! I’d love to hear and maybe cover them in a follow-up 🚀
Any questions? I’m happy to help!
Connect with me:
🔗 LinkedIn: https://www.linkedin.com/in/mahdi-shamlou-3b52b8278
📱 Telegram: https://telegram.me/mahdi0shamlou
📸 Instagram: https://www.instagram.com/mahdi0shamlou/
Author: Mahdi Shamlou | مهدی شاملو

Top comments (0)