Add two Numbers — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

Understanding Add two Numbers
Detailed explanation and reference materials
Problem Overview

Problem: Add Two Numbers Represented by Linked Lists

Difficulty: Medium
Topics: Linked Lists, Mathematical Computation, Algorithm Design
Objective: Add two non-negative integers represented by linked lists and return the result as a linked list.


Problem Description

You are given two non-negative integers represented by two linked lists, where each node contains a single digit. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

Example:

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

Output:
[7, 0, 8]

Explanation:
342 + 465 = 807. The sum is returned as a linked list, where the digits are reversed.


Code Explanation

Step-by-Step Breakdown

The code defines a C++ class called Solution with a member function addTwoNumbers. The function adds two numbers represented by linked lists and returns the result as a new linked list.

1. Initialization:

  • A dummy head node (dummyHead) is created to simplify list construction.
  • A pointer (l3) is initialized to point to dummyHead.

2. Addition Loop:

  • The loop continues as long as either of the linked lists has more digits or there's a carry left.
  • For each iteration, the digits from l1 and l2 are extracted (or treated as 0 if the list is shorter).
  • The sum is computed, and the carry is updated.

3. Creating the Result List:

  • A new node with the current digit is created and appended to the result list.
  • The pointer l3 is moved forward, and l1 and l2 are also advanced to the next nodes.

4. Final Result:

  • The result starts from the node following the dummy head.
  • The dummy head is deleted, and the final result is returned.

Test Cases

Test Case 1: Standard Addition

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

Output:
[7, 0, 8]

Explanation:
342 + 465 = 807, which is returned as a linked list: [7, 0, 8].


Test Case 2: Addition with Carry Over

Input:
l1 = [9, 9, 9], l2 = [1, 1, 1]

Output:
[0, 1, 1, 1]

Explanation:
999 + 111 = 1110, which is returned as a linked list: [0, 1, 1, 1].


Test Case 3: Unequal Length Lists

Input:
l1 = [1, 2], l2 = [9, 9, 9]

Output:
[0, 2, 0, 1]

Explanation:
12 + 999 = 1011, which is returned as a linked list: [0, 2, 0, 1].


Test Case 4: One List is Empty

Input:
l1 = [], l2 = [5, 6, 4]

Output:
[5, 6, 4]

Explanation:
If one list is empty, the other list is returned as the result.


Test Case 5: Both Lists are Empty

Input:
l1 = [], l2 = []

Output:
[]

Explanation:
If both lists are empty, the result is an empty list.


— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
linked-list
java
Reference Link
https://leetcode.com/problems/add-two-numbers/

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly 🧩

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ?  

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

🔒 Locked steps cannot be moved. Only unlocked steps are draggable.

🔊 Enable sound for swap feedback and completion effects.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.