Skip to interview prep
Apexed

84. Add Two Numbers Stored as Lists

MediumLinked lists

Two non-negative integers are stored as linked lists of digits, with the least significant digit first (so 342 is written [2, 4, 3]). Return their sum as a list in the same format.

Neither number has leading zeros, except the number 0 itself.

A linked list is passed as ListNode objects with val and next. The tests write lists as arrays.

Example 1

Input: a = [2, 4, 3], b = [5, 6, 4]
Output: [7, 0, 8]
Explanation: 342 + 465 = 807

Constraints

  • 1 ≤ nodes in each list ≤ 100
  • Each value is a digit 0–9

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

Add Two Numbers Stored as Lists — coding interview practice — Apexed