LeetCode 2: Add Two Numbers – Step-by-Step Linked List Addition Explained Clearly
LeetCode 2: Add Two Numbers – Step-by-Step Linked List Addition Explained Clearly

LeetCode 2: Add Two Numbers – Step-by-Step Linked List Addition Explained Clearly

Learn How to Add Numbers Represented as Linked Lists Using Carry Logic, Dummy Nodes, and Iterative Traversal (Beginner Friendly Guide)

3 views
0
0

Try the Problem

You can practice the problem here:

https://leetcode.com/problems/add-two-numbers/


Problem Description (In Simple Words)

You are given two linked lists, where:

  1. Each node contains a single digit (0–9)
  2. The digits are stored in reverse order

👉 That means:

[2,4,3] represents 342
[5,6,4] represents 465

Your task is:

👉 Add these two numbers

👉 Return the result as a linked list (also in reverse order)


Important Points to Understand

  1. Each node contains only one digit
  2. Numbers are stored in reverse order
  3. You must return the answer as a linked list
  4. You must handle carry just like normal addition


Example Walkthrough

Example 1

Input:

l1 = [2,4,3]
l2 = [5,6,4]

Interpretation:

342 + 465 = 807

Output:

[7,0,8]

Example 2

Input:

l1 = [0]
l2 = [0]

Output:

[0]

Example 3

Input:

l1 = [9,9,9,9,9,9,9]
l2 = [9,9,9,9]

Output:

[8,9,9,9,0,0,0,1]


Constraints

1 <= number of nodes <= 100
0 <= Node.val <= 9
No leading zeros except the number 0


Understanding the Core Idea

This problem is essentially:

👉 Adding two numbers digit by digit

But instead of arrays or integers, the digits are stored in linked lists.


How Addition Works (Real-Life Analogy)

Let’s add:

342 + 465

We normally add from right to left:

2 + 5 = 7
4 + 6 = 10 → write 0, carry 1
3 + 4 + 1 = 8

Now notice something important:

👉 Linked list is already in reverse order

👉 So we can directly process from left to right


Thinking About the Solution

Before coding, let’s think of possible approaches:

Possible Ways to Solve

  1. Convert linked lists into numbers → add → convert back (Not safe due to overflow)
  2. Store digits in arrays and simulate addition
  3. Traverse both lists and simulate addition directly (best approach)


Optimal Approach: Simulate Addition (Digit by Digit)

Key Idea

We traverse both linked lists and:

  1. Add corresponding digits
  2. Maintain a carry
  3. Create a new node for each digit of result

Step-by-Step Logic

Step 1: Initialize

  1. Create a dummy node (to simplify result building)
  2. Create a pointer curr to build the result list
  3. Initialize carry = 0

Step 2: Traverse Both Lists

Continue while:

l1 != null OR l2 != null

At each step:

  1. Start with carry:
sum = carry
  1. Add value from l1 (if exists)
  2. Add value from l2 (if exists)

Step 3: Create New Node

  1. Digit to store:
sum % 10
  1. New carry:
sum / 10

Step 4: Move Forward

  1. Move l1 and l2 if they exist
  2. Move curr forward

Step 5: Handle Remaining Carry

If carry is not zero after loop:

👉 Add one more node

Step 6: Return Result

Return:

dummy.next


Code Implementation (With Explanation)

class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {

// Dummy node to simplify result construction
ListNode dumm = new ListNode(-1);
ListNode curr = dumm;

int sum = 0;
int carr = 0;

// Traverse both lists
while(l1 != null || l2 != null){

// Start with carry
sum = carr;

// Add l1 value if exists
if(l1 != null){
sum += l1.val;
l1 = l1.next;
}

// Add l2 value if exists
if(l2 != null){
sum += l2.val;
l2 = l2.next;
}

// Create new node with digit
ListNode newNode = new ListNode(sum % 10);

// Update carry
carr = sum / 10;

// Attach node
curr.next = newNode;
curr = curr.next;
}

// If carry remains
if(carr != 0){
curr.next = new ListNode(carr);
}

return dumm.next;
}
}


Dry Run (Important for Understanding)

Input:

l1 = [2,4,3]
l2 = [5,6,4]

Step-by-step:

Step 1:
2 + 5 = 7 → node(7), carry = 0

Step 2:
4 + 6 = 10 → node(0), carry = 1

Step 3:
3 + 4 + 1 = 8 → node(8), carry = 0

Final:

7 → 0 → 8


Time Complexity

O(max(n, m))

Where:

  1. n = length of l1
  2. m = length of l2


Space Complexity

O(max(n, m))

For storing the result linked list.


Why Dummy Node is Important

Without dummy node:

  1. You need to handle first node separately

With dummy node:

  1. Code becomes clean and consistent
  2. No special cases required


Common Mistakes to Avoid

❌ Forgetting to handle carry

❌ Not checking if l1 or l2 is null

❌ Missing last carry node

❌ Incorrect pointer movement


Key Takeaways

  1. This is a simulation problem
  2. Linked list problems become easier with dummy nodes
  3. Always think in terms of real-world analogy (addition)


Conclusion

The Add Two Numbers problem is one of the most important linked list problems for interviews.

It teaches:

  1. Pointer manipulation
  2. Carry handling
  3. Iterative traversal
  4. Clean code using dummy nodes

Once you understand this pattern, many other linked list problems become much easier to solve.

👉 Tip: Whenever you see problems involving numbers in linked lists, think of digit-by-digit simulation with carry.

Ai Assistant Kas