DSA · Linked List · #34
Add Two Numbers
Module 42 · difficulty 3/5·⏱ 30:00starts on first keystroke
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each node contains a single digit. Add the two numbers and return the sum as a linked list, also in reverse order. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Implement `addTwoNumbers(l1, l2)` where each list is a chain of nodes `{ val, next }` (with `next` defaulting to `null`). Return the head node of the resulting list.
Examples
l1 = [2,4,3], l2 = [5,6,4]→[7,0,8]— 342 + 465 = 807, stored reversed.l1 = [0], l2 = [0]→[0]l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]→[8,9,9,9,0,0,0,1]— 9999999 + 9999 = 10009998; a final carry adds an extra node.
Constraints
- · The number of nodes in each 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.
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.