Add Two Numbers

Medium#2dummy head

Two whole numbers, 0 or more, are given as linked lists of their digits, the ones digit first: 957 is 7 → 5 → 9. Return their sum as a list of the same kind. Our example: 7 → 5 → 9 (957) and 6 → 8 (86) — the answer is 3 → 4 → 0 → 1, that is 1043.

input
l1, l2: two non-negative integers as linked lists of their digits, the ones digit first.
returns
Their sum, as a linked list of its digits the same way.
constraints
  • 1 ≤ nodes in each list ≤ 100
  • 0 ≤ Node.val ≤ 9
  • no leading zeros, but in the number 0 itself
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
function addTwoNumbers(l1, l2) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more linked list pointers problems

Pattern “Linked list pointers” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.