By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(Complete Guide for FAANG-Level Interviews)
"This problem appears in 1 out of every 3 onsite interviews—master it, and you prove you can handle edge cases, pointer manipulation, and clean code under pressure."
Before tackling this problem, ensure you understand: 1. Singly Linked List Basics (traversal, dummy nodes, pointer updates). 2. Carry Propagation (how addition overflow works in base-10). 3. Time/Space Complexity (O(n) time, O(1) or O(n) space trade-offs).
dummy = ListNode(0); current = dummy
carry = sum_val // 10; digit = sum_val % 10
while l1 or l2 or carry:
while l1 and l2
l1 = l1.next if l1 else None
if carry: current.next = ListNode(carry)
(Repeatable mental checklist for every linked-list addition problem.)
val1 = l1.val if l1 else 0
val2 = l2.val if l2 else 0
sum_val = val1 + val2 + carry
carry = sum_val // 10
current.next = ListNode(sum_val % 10)
l2 = l2.next if l2 else None
current = current.next
999 + 1 = 1000
Problem: Two non-empty linked lists represent non-negative integers in reverse order. Add them and return the sum as a linked list. Input: l1 = [2,4,3], l2 = [5,6,4] (represents 342 + 465 = 807) Output: [7,0,8]
l1 = [2,4,3]
l2 = [5,6,4]
[7,0,8]
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def addTwoNumbers(l1: ListNode, l2: ListNode) -> ListNode: dummy = ListNode(0) current = dummy carry = 0 while l1 or l2 or carry: val1 = l1.val if l1 else 0 val2 = l2.val if l2 else 0 sum_val = val1 + val2 + carry carry = sum_val // 10 current.next = ListNode(sum_val % 10) current = current.next l1 = l1.next if l1 else None l2 = l2.next if l2 else None return dummy.next
while l1 or l2 or carry
Problem: Numbers are stored in forward order (e.g., [7,2,4,3] = 7243). Return the sum in the same order. Input: l1 = [7,2,4,3], l2 = [5,6,4] (7243 + 564 = 7807) Output: [7,8,0,7]
[7,2,4,3]
l1 = [7,2,4,3]
[7,8,0,7]
def addTwoNumbers(l1: ListNode, l2: ListNode) -> ListNode: stack1, stack2 = [], [] # Push all digits to stacks while l1: stack1.append(l1.val) l1 = l1.next while l2: stack2.append(l2.val) l2 = l2.next carry = 0 result = None while stack1 or stack2 or carry: val1 = stack1.pop() if stack1 else 0 val2 = stack2.pop() if stack2 else 0 sum_val = val1 + val2 + carry carry = sum_val // 10 # Prepend new node (reverse order) new_node = ListNode(sum_val % 10) new_node.next = result result = new_node return result
Problem: Modify one of the input lists to store the result (no extra space). Constraint: Cannot create new nodes; must reuse existing ones.
def reverseList(head: ListNode) -> ListNode: prev = None while head: next_node = head.next head.next = prev prev = head head = next_node return prev def addTwoNumbers(l1: ListNode, l2: ListNode) -> ListNode: # Reverse both lists l1 = reverseList(l1) l2 = reverseList(l2) dummy = ListNode(0) current = dummy carry = 0 while l1 or l2 or carry: val1 = l1.val if l1 else 0 val2 = l2.val if l2 else 0 sum_val = val1 + val2 + carry carry = sum_val // 10 # Reuse l1's nodes (or l2's) if l1: l1.val = sum_val % 10 current.next = l1 l1 = l1.next else: current.next = ListNode(sum_val % 10) current = current.next # Reverse the result back return reverseList(dummy.next)
if l1
if l2
val = l1.val if l1 else 0
current
dummy.next
123
[9,9,9]
[1]
"Here’s your 60-second cheat sheet for linked-list addition problems:
Test your code with: - Empty lists. - Carry at the end (e.g., 999 + 1). - One list longer than the other.
999 + 1
Now go crush that interview!"
[] + [1]
[9,9] + [1]
[0] + [0]
This framework is directly usable in any FAANG interview. Good luck! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.