Add Two Numbers
Medium Linked List AI round
l1 and l2 are the heads of two singly linked lists. Each holds a whole number of 0 or more, one digit a node, the ones digit first, and a number has no zero at its end: only 0 itself is a single node holding 0.
Add the two numbers.
Return the head of a list that holds the sum the same way: one digit a node, the ones digit first, and no zero at its end unless the sum is 0.
Example 1
Input: l1 = [4,0,8], l2 = [8,6]
Output: [2,7,8]
l1 holds 804 and l2 holds 68, each ones digit first, and 804 + 68 = 872. The ones column makes 12, so 2 stays and 1 is carried into the tens, where 0 + 6 + 1 makes 7.
Example 2
Input: l1 = [6,4], l2 = [7,8]
Output: [3,3,1]
Example 3
Input: l1 = [0], l2 = [3,1]
Output: [3,1]
Constraints
1 ≤ number of nodes in each list ≤ 10⁵0 ≤ node.val ≤ 9- The last node of a list is not 0, unless the list is the single node 0
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
Watch the explanation
More Linked List
- Merge Two Sorted ListsAI round · Easy
- Reverse Linked ListAI round · Very easy
- Swap Nodes in PairsAI round · Medium
- Odd Even Linked ListAI round · Medium
- Convert Binary Search Tree to Sorted Doubly Linked ListMedium