PPractick

DSA interview question

Add Two Numbers Represented by Linked Lists

Medium · Data structures · Topics: linkedlist, math

The problem

A calculator for arbitrarily large integers stores each number as a linked list of digits in reverse order, least significant digit first, so 2 -> 4 -> 3 is 342. Adding two such numbers is schoolbook addition, and the reverse order is exactly what makes it easy: walk both lists together from the head, add digit by digit with a carry, and build the result list as you go. The only details are lists of different lengths and a final carry that adds one extra digit.

Example: 2 -> 4 -> 3 (342) + 5 -> 6 -> 4 (465) → 7 -> 0 -> 8 (807).

Function signature

Node* addTwoNumbers(Node* l1, Node* l2);  // digits in reverse order; returns a new list

Test cases

Input (l1, l2)OutputExplanation
(2 -> 4 -> 3, 5 -> 6 -> 4)7 -> 0 -> 8342 + 465 = 807
(0, 0)0
(9 -> 9 -> 9 -> 9 -> 9 -> 9 -> 9, 9 -> 9 -> 9 -> 9)8 -> 9 -> 9 -> 9 -> 0 -> 0 -> 0 -> 1Different lengths and a final carry
(1, 9 -> 9)0 -> 0 -> 11 + 99 = 100
(5, 5)0 -> 1The final carry creates a new digit

(Confirm: digits are stored least significant first; the inputs have no leading zeros except the number 0; the result is a new list.)

How interviewers phrase it

Clarifying questions to ask

  1. Are the digits stored in reverse (least significant first)?
  2. Can the lists have different lengths?
  3. Should I create a new list or reuse the input nodes?

Follow-ups to expect

  1. "How do you handle different lengths?"
  2. "When does the loop stop?"
  3. "Why a dummy head?"
  4. "What if the digits were most significant first?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in