Bird
Raised Fist0
DSA C++programming~20 mins

Add Two Numbers Represented as Linked List in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Addition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Adding Two Numbers in Linked List Form
What is the output linked list after adding these two numbers represented as linked lists?

List 1: 2 -> 4 -> 3 -> null (represents 342)
List 2: 5 -> 6 -> 4 -> null (represents 465)

Use the standard addition where digits are stored in reverse order.
DSA C++
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode* dummy = new ListNode(0);
    ListNode* current = dummy;
    int carry = 0;
    while (l1 || l2 || carry) {
        int sum = carry;
        if (l1) {
            sum += l1->val;
            l1 = l1->next;
        }
        if (l2) {
            sum += l2->val;
            l2 = l2->next;
        }
        carry = sum / 10;
        current->next = new ListNode(sum % 10);
        current = current->next;
    }
    return dummy->next;
}
A7 -> 0 -> 8 -> null
B7 -> 0 -> 7 -> null
C8 -> 0 -> 7 -> null
D7 -> 1 -> 8 -> null
Attempts:
2 left
💡 Hint
Add digits from the head, carry over if sum >= 10.
❓ Predict Output
intermediate
2:00remaining
Result of Adding Unequal Length Linked Lists
What is the output linked list after adding these two numbers represented as linked lists?

List 1: 9 -> 9 -> 9 -> 9 -> null (represents 9999)
List 2: 1 -> null (represents 1)

Digits are stored in reverse order.
DSA C++
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode* dummy = new ListNode(0);
    ListNode* current = dummy;
    int carry = 0;
    while (l1 || l2 || carry) {
        int sum = carry;
        if (l1) {
            sum += l1->val;
            l1 = l1->next;
        }
        if (l2) {
            sum += l2->val;
            l2 = l2->next;
        }
        carry = sum / 10;
        current->next = new ListNode(sum % 10);
        current = current->next;
    }
    return dummy->next;
}
A1 -> 0 -> 0 -> 0 -> 0 -> null
B0 -> 0 -> 0 -> 0 -> null
C0 -> 0 -> 0 -> 0 -> 1 -> null
D0 -> 0 -> 0 -> 1 -> null
Attempts:
2 left
💡 Hint
Remember to carry over after last digit.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Linked List Addition Code
What error will this code produce when adding two linked lists?

Code snippet:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* dummy = new ListNode(0); ListNode* current = dummy; int carry = 0; while (l1 != nullptr && l2 != nullptr) { int sum = l1->val + l2->val + carry; carry = sum / 10; current->next = new ListNode(sum % 10); current = current->next; l1 = l1->next; l2 = l2->next; } return dummy->next; }
DSA C++
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode* dummy = new ListNode(0);
    ListNode* current = dummy;
    int carry = 0;
    while (l1 != nullptr && l2 != nullptr) {
        int sum = l1->val + l2->val + carry;
        carry = sum / 10;
        current->next = new ListNode(sum % 10);
        current = current->next;
        l1 = l1->next;
        l2 = l2->next;
    }
    return dummy->next;
}
AThe code produces incorrect sum because carry is not updated.
BThe code causes a segmentation fault due to null pointer dereference.
CThe code runs correctly and returns the correct sum.
DThe code misses adding remaining digits if lists are unequal length.
Attempts:
2 left
💡 Hint
Check what happens after the while loop ends.
❓ Predict Output
advanced
2:00remaining
Output of Adding Large Numbers with Carry
What is the output linked list after adding these two numbers?

List 1: 9 -> 8 -> 7 -> 6 -> 5 -> null (represents 56789)
List 2: 1 -> 2 -> 3 -> 4 -> 5 -> null (represents 54321)

Digits stored in reverse order.
DSA C++
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};

ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode* dummy = new ListNode(0);
    ListNode* current = dummy;
    int carry = 0;
    while (l1 || l2 || carry) {
        int sum = carry;
        if (l1) {
            sum += l1->val;
            l1 = l1->next;
        }
        if (l2) {
            sum += l2->val;
            l2 = l2->next;
        }
        carry = sum / 10;
        current->next = new ListNode(sum % 10);
        current = current->next;
    }
    return dummy->next;
}
A0 -> 1 -> 1 -> 1 -> 1 -> 1 -> null
B0 -> 1 -> 1 -> 1 -> 1 -> null
C0 -> 1 -> 1 -> 1 -> 0 -> 1 -> null
D0 -> 1 -> 1 -> 1 -> 0 -> null
Attempts:
2 left
💡 Hint
Add digit by digit with carry, watch for final carry digit.
🧠 Conceptual
expert
2:00remaining
Why Use a Dummy Node in Linked List Addition?
Why do we use a dummy node at the start of the linked list when adding two numbers represented as linked lists?
ATo store the carry value separately from the sum digits.
BTo simplify code by avoiding special cases for the head node and easily return the result list.
CTo reverse the linked list after addition is complete.
DTo keep track of the length of the resulting linked list.
Attempts:
2 left
💡 Hint
Think about how linked lists are built node by node.

Practice

(1/5)
1. What does the problem "Add Two Numbers Represented as Linked List" mainly involve?
easy
A. Adding two numbers where each digit is stored in a node of a linked list
B. Sorting two linked lists in ascending order
C. Finding the middle node of a linked list
D. Reversing a linked list

Solution

  1. Step 1: Understand the problem context

    The problem involves two linked lists where each node contains a single digit of a number, stored in reverse order.
  2. Step 2: Identify the required operation

    We need to add these two numbers digit by digit, handling carry, and return the sum as a linked list.
  3. Final Answer:

    Adding two numbers where each digit is stored in a node of a linked list -> Option A
  4. Quick Check:

    Adding digits in linked lists = Adding two numbers where each digit is stored in a node of a linked list [OK]
Hint: Think of linked list nodes as digits of a number [OK]
Common Mistakes:
  • Confusing addition with sorting
  • Thinking the problem is about reversing lists
  • Assuming the digits are stored in normal order
2. Which of the following C++ code snippets correctly declares a node structure for a singly linked list used in this problem (using nullptr)?
easy
A. struct Node { int val; Node* next; Node(int x) : val(x), next(NULL) {} };
B. struct Node { int val; Node next; Node(int x) : val(x), next(nullptr) {} };
C. struct Node { int val; Node* next; Node(int x) : val(x), next(nullptr) {} };
D. struct Node { int val; Node* next; Node(int x) { val = x; next = NULL; } };

Solution

  1. Step 1: Check pointer usage in struct

    Node* next is correct to point to the next node; struct Node { int val; Node next; Node(int x) : val(x), next(nullptr) {} }; uses Node next which is incorrect (causes infinite recursion).
  2. Step 2: Check constructor initialization

    struct Node { int val; Node* next; Node(int x) : val(x), next(nullptr) {} }; uses member initializer list correctly with nullptr; struct Node { int val; Node* next; Node(int x) { val = x; next = NULL; } }; uses assignment inside constructor body which is valid but less modern; struct Node { int val; Node* next; Node(int x) : val(x), next(NULL) {} }; uses NULL which is legacy.
  3. Final Answer:

    struct Node { int val; Node* next; Node(int x) : val(x), next(nullptr) {} }; -> Option C
  4. Quick Check:

    Use pointer and nullptr in constructor [OK]
Hint: Use Node* next and initialize with nullptr [OK]
Common Mistakes:
  • Using Node next instead of Node* next
  • Using NULL instead of nullptr
  • Not initializing next pointer
3. What is the output linked list after adding the two numbers represented by these linked lists? List1: 2 -> 4 -> 3 -> null (represents 342) List2: 5 -> 6 -> 4 -> null (represents 465) Choose the correct resulting linked list:
medium
A. 7 -> 10 -> 7 -> null
B. 7 -> 0 -> 8 -> null
C. 8 -> 0 -> 7 -> null
D. 7 -> 1 -> 8 -> null

Solution

  1. Step 1: Add digits node by node with carry

    2+5=7 (carry 0), 4+6=10 (carry 1), 3+4+1=8 (carry 0)
  2. Step 2: Form the resulting linked list

    Digits are 7, 0 (from 10 carry), 8 so list is 7 -> 0 -> 8 -> null
  3. Final Answer:

    7 -> 0 -> 8 -> null -> Option B
  4. Quick Check:

    342 + 465 = 807 represented as 7->0->8 [OK]
Hint: Add digits with carry, store remainder in node [OK]
Common Mistakes:
  • Not handling carry correctly
  • Adding digits in wrong order
  • Confusing node values with actual numbers
4. Consider the following code snippet for adding two numbers represented as linked lists. What is the error?
Node* addTwoNumbers(Node* l1, Node* l2) {
  Node* dummy = new Node(0);
  Node* current = dummy;
  int carry = 0;
  while (l1 != nullptr && l2 != nullptr) {
    int sum = l1->val + l2->val + carry;
    carry = sum / 10;
    current->next = new Node(sum % 10);
    current = current->next;
    l1 = l1->next;
    l2 = l2->next;
  }
  return dummy->next;
}
medium
A. The loop condition should be while (l1 != nullptr || l2 != nullptr)
B. Carry is not updated correctly
C. Dummy node should be deleted before return
D. The sum calculation is incorrect

Solution

  1. Step 1: Analyze loop condition

    The loop runs only while both l1 and l2 are not null, so it stops early if one list is longer.
  2. Step 2: Correct loop condition

    It should continue while either l1 or l2 has nodes to process all digits, so use || instead of &&.
  3. Final Answer:

    The loop condition should be while (l1 != nullptr || l2 != nullptr) -> Option A
  4. Quick Check:

    Use || to handle different length lists [OK]
Hint: Use || in loop to cover all nodes [OK]
Common Mistakes:
  • Using && causes missing nodes in longer list
  • Ignoring carry after loop ends
  • Not advancing pointers correctly
5. Given two linked lists representing numbers in reverse order, how would you modify the addition algorithm to handle the case when the final carry is non-zero after processing all nodes? Choose the best approach:
hard
A. Ignore the carry since it is not needed
B. Subtract the carry from the last node value
C. Restart the addition from the beginning
D. Add a new node with the carry value at the end of the result list

Solution

  1. Step 1: Understand carry after full addition

    After adding all nodes, if carry is still 1, it means the sum has an extra digit.
  2. Step 2: Add new node for carry

    To represent this extra digit, create a new node with carry value and link it at the end.
  3. Final Answer:

    Add a new node with the carry value at the end of the result list -> Option D
  4. Quick Check:

    Carry leftover means add new node [OK]
Hint: Add carry node if leftover after loop ends [OK]
Common Mistakes:
  • Ignoring leftover carry causing wrong sum
  • Trying to subtract carry instead of adding node
  • Restarting addition unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard