38. Add Two Numbers
Medium · Linked List
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.
The input linked lists may have different lengths. You must handle carrying correctly as you add digit by digit from the least significant (head) to the most significant (tail).
Examples
Example 1 Input: l1 = [2, 4, 3], l2 = [5, 6, 4] Output: [7, 0, 8] Explanation: 342 + 465 = 807. The lists are in reverse order: 2→4→3 represents 342, and 5→6→4 represents 465. The result 7→0→8 represents 807.
Example 2 Input: l1 = [0], l2 = [0] Output: [0] Explanation: 0 + 0 = 0. Both lists contain a single node with value 0.
Constraints
- Standard input/output constraints apply