11. Add Two Numbers
mediumAsked at RevolutAdd two big integers represented as linked lists in reverse order, a Revolut staple that mirrors summing two arbitrary-precision currency amounts limb by limb.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Problem
You are given two non-empty linked lists representing two non-negative integers stored in reverse order, each node holding a single digit. Add the two numbers and return the sum as a linked list.
Constraints
Nodes per list in [1, 100]0 <= Node.val <= 9Inputs have no leading zeros except 0 itself
Examples
Example 1
l1=[2,4,3], l2=[5,6,4][7,0,8]Example 2
l1=[0], l2=[0][0]Approaches
1. Convert to BigInt and back
Decode lists into BigInts, add, encode back.
- Time
- O(n)
- Space
- O(n)
// decode l1, l2 into BigInts, add, re-emit digits into a new listTradeoff:
2. Limb-wise carry walk
Walk both lists in lockstep summing digit+digit+carry, emit one node per step. Linear time and space.
- Time
- O(max(m,n))
- Space
- O(max(m,n))
function addTwoNumbers(l1, l2){
const head = { val: 0, next: null };
let tail = head, carry = 0;
while (l1 || l2 || carry){
const s = (l1?.val||0) + (l2?.val||0) + carry;
carry = Math.floor(s/10);
tail.next = { val: s%10, next: null };
tail = tail.next;
l1 = l1?.next; l2 = l2?.next;
}
return head.next;
}Tradeoff:
Revolut-specific tips
Revolut grades for explicit handling of the dangling carry node — call out that this is how a real multi-currency settlement engine handles overflow into the next minor unit.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
Practice these live with InterviewChamp.AI
Drill Add Two Numbers and other Revolut interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →