Add Two Numbers – A Clean and Clear Guide

shaik rasoolshaik rasool
3 min read

πŸ“˜ 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?
    Number 123 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

OperationComplexity
TimeO(max(m, n)) where m and n are lengths of the lists
SpaceO(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)


  • 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.

0
Subscribe to my newsletter

Read articles from shaik rasool directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

shaik rasool
shaik rasool