← DiffPush

Add two LL

Standard Bar Linked List · Medium Problems of LL O(max(N, M)) · O(max(N, M))

Merging Two Ledger Chains Digit by Digit

Two accounting terminals store big balances as digit chains, least significant digit first — the one layout where addition can start immediately at the head. A reconciliation pass walks both chains together, sums paired digits plus the running carry, emits each sum digit, and appends one final digit if a carry outlasts both chains.

Input: Two arrays nums1 and nums2 of digits 0-9, each representing a non-negative integer with the least significant digit first.

Output: The digit sequence of the sum, least significant digit first.

Constraints

Examples

Example 1

Input: {"nums1":[2,4,3],"nums2":[5,6,4]}
Output: [7,0,8]
342 + 465 = 807, emitted least significant digit first as 7, 0, 8.

Example 2

Input: {"nums1":[0],"nums2":[0]}
Output: [0]
0 + 0 = 0 — a single zero digit, no carry anywhere.

Solve this in your browser →

Also on LeetCode ↗