Skip to main content

2. Add Two Numbers

mediumAsked at Cisco

Add Two Numbers at Cisco is a linked-list problem that mirrors big-integer addition. The interviewer is checking whether you handle the carry propagation correctly and whether you use a dummy-head node to avoid edge-case branching on the first append.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Cisco loops.

  • Glassdoor (2026-Q1)Cisco SDE-I and SDE-II interview reports list Add Two Numbers as a 30-minute coding round.
  • Blind (2025-09)Cisco interview thread cites Add Two Numbers as a recurring linked-list problem.

Problem

You are given two non-empty linked lists representing two non-negative integers. 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. You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Constraints

  • The number of nodes in each linked list is in the range [1, 100].
  • 0 <= Node.val <= 9
  • It is guaranteed that the list represents a number that does not have leading zeros.

Examples

Example 1

Input
l1 = [2,4,3], l2 = [5,6,4]
Output
[7,0,8]

Explanation: 342 + 465 = 807; reversed digit order: 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]

Approaches

1. Brute-force: convert to integer, add, convert back

Walk both lists, build the numeric values, add, then re-encode the digits.

Time
O(n + m)
Space
O(n + m)
function addTwoNumbersBrute(l1, l2) {
  function toBigInt(node) {
    let n = 0n, place = 1n;
    while (node) {
      n += BigInt(node.val) * place;
      place *= 10n;
      node = node.next;
    }
    return n;
  }
  let sum = toBigInt(l1) + toBigInt(l2);
  if (sum === 0n) return { val: 0, next: null };
  let dummy = { val: 0, next: null };
  let cur = dummy;
  while (sum > 0n) {
    cur.next = { val: Number(sum % 10n), next: null };
    cur = cur.next;
    sum /= 10n;
  }
  return dummy.next;
}

Tradeoff: Correct, but requires BigInt because the lists could represent integers larger than Number.MAX_SAFE_INTEGER. Cisco interviewers want the in-place digit-by-digit addition because it generalizes to arbitrary precision without language-specific tricks.

2. Digit-by-digit addition with carry (optimal)

Walk both lists in lockstep. At each position: sum = l1.val + l2.val + carry. New digit = sum % 10. New carry = floor(sum / 10). Use a dummy-head node to simplify the first append.

Time
O(max(n, m))
Space
O(max(n, m))
function addTwoNumbers(l1, l2) {
  const dummy = { val: 0, next: null };
  let cur = dummy;
  let carry = 0;
  while (l1 || l2 || carry) {
    const v1 = l1 ? l1.val : 0;
    const v2 = l2 ? l2.val : 0;
    const sum = v1 + v2 + carry;
    carry = Math.floor(sum / 10);
    cur.next = { val: sum % 10, next: null };
    cur = cur.next;
    if (l1) l1 = l1.next;
    if (l2) l2 = l2.next;
  }
  return dummy.next;
}

Tradeoff: The canonical answer. The dummy-head node is the trick — without it, the first append needs a branch ('is result null yet?'). The `while (l1 || l2 || carry)` is the gotcha: don't forget to continue while carry is non-zero even after both lists end (the 9999 + 9 case).

Cisco-specific tips

Cisco interviewers grade this on the dummy-head pattern AND the carry loop condition. State both out loud: 'I use a dummy-head node so I never have a special-case for the first append. The loop continues while EITHER list still has nodes OR there's a non-zero carry — that last condition is what handles the 999 + 1 case correctly.' That sentence is the whole interview signal.

Common mistakes

  • Forgetting the `|| carry` in the loop condition — 999 + 1 returns [0, 0, 0] instead of [0, 0, 0, 1].
  • Not using a dummy-head node — the first append needs an `if (!result) result = node; else cur.next = node;` branch and the code triples in length.
  • Reading l1.next / l2.next when l1 or l2 is null — must guard with `if (l1) l1 = l1.next`.

Follow-up questions

An interviewer at Cisco may pivot to one of these next:

  • Add Two Numbers II (LC 445) — digits stored in FORWARD order; reverse both lists or use stacks.
  • Multiply Strings (LC 43) — same digit-by-digit pattern but for multiplication.
  • Plus One Linked List (LC 369 premium) — increment a number stored as a linked list.
  • What if the lists are very long (10^7 nodes)? — same algorithm, just memory budget consideration.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why the dummy-head node?

Because without it, the first append needs special handling: 'if result is null, start with this node; otherwise append.' The dummy-head removes that branch — you always do `cur.next = newNode; cur = cur.next;` and return `dummy.next` at the end. Saves 4 lines AND removes a class of off-by-one bugs.

Why is reverse-digit-order convenient?

Because addition starts from the least significant digit, where carries propagate from. The reverse order lets you walk both lists in lockstep without first reversing or finding the lengths. LC 445 (forward order) is genuinely harder for exactly this reason.

Free learning resources

Curated free links for this problem.

Practice these live with InterviewChamp.AI

Drill Add Two Numbers and other Cisco interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →