Stadium Gate Tallies
A stadium counts fans at its north and south gates with old counters that store a number as a chain of digits, ones digit first: 4,071 fans is 1 → 7 → 0 → 4. A tally can be too long for any built-in integer type to hold.
At the end of the night the stadium needs the total for both gates, stored in the same format.
Your task
Write addTallies(north, south). Each argument is a non-empty linked list of digits (0 to 9), ones digit first. Return a linked list holding the sum, in the same format.
The lists may have different lengths. Neither has extra zeros at the end of the chain (its highest digit), except the number 0 itself, which is [0]. The result must follow the same rule.
addTallies(north: ListNode, south: ListNode) → ListNode
Examples
north = [1, 7, 0, 4] south = [9, 5, 6]
[0, 3, 7, 4]
The lists are 4,071 and 659, and their sum is 4,730. In the ones place, 1 + 9 = 10, so you write 0 and carry 1. In the tens place, 7 + 5 + 1 = 13, so you write 3 and carry 1. In the hundreds place, 0 + 6 + 1 = 7, and the thousands place is just 4. Written ones digit first, the result is 0, 3, 7, 4.
north = [8, 9] south = [4]
[2, 0, 1]
The lists are 98 and 4, and their sum is 102. In the ones place, 8 + 4 = 12, so you write 2 and carry 1. In the tens place, 9 + 1 = 10, so you write 0 and carry 1. Both lists are finished but a carry is left, so it becomes a new final digit, 1.
Constraints
- 1 ≤ length of each list ≤ 100
- 0 ≤ each digit ≤ 9
- Numbers can have up to 100 digits, so do not convert them to built-in numbers.
Start with brute force
Turn each chain into a number, add, and turn the result back into a chain.
That breaks past about 16 digits: JavaScript numbers lose precision beyond 9,007,199,254,740,991, and many languages overflow at 64 bits. Python's integers grow without limit, so this approach works there, but it avoids the digit-by-digit addition that this problem is meant to practice.
Hints
0 of 4 shown- Hint 1
Add 98 + 4 on paper. Which digit do you start with, and in which direction do you move?
- Hint 2
Paper addition starts at the ones and moves left with a carry. The lists store the ones digit first, so walking forwards is the paper order.
- Hint 3
Each position adds a digit from each list (0 if that list has run out) and the carry. What if both lists end with the carry still 1?
- Hint 4
Walk both lists with
carry= 0. While either list has nodes orcarryis not 0:sum = digitA + digitB + carry, append a node withsum % 10, setcarry = floor(sum / 10). Build the result with a dummy head and a tail pointer.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
With the ones digit first, walking the chain forwards is paper-addition order. So you do paper addition one node at a time and carry as you go.
Trace 1 → 7 → 0 → 4 plus 9 → 5 → 6:
- 1 + 9 + carry 0 = 10, so write 0 and carry 1.
- 7 + 5 + 1 = 13, so write 3 and carry 1.
- 0 + 6 + 1 = 7, so write 7 and carry 0.
- The south list has ended, so its digit counts as 0. 4 + 0 + 0 = 4, so write 4.
- Both lists have ended and the carry is 0, so the loop stops. The result is
0 → 3 → 7 → 4, which is 4,730.
The loop condition includes carry. Without it, 98 + 4 would stop at 2 → 0 and lose the final 1, which is the most common bug in this problem.
function addTallies(north, south) {
const dummy = new ListNode(0, null);
let tail = dummy;
let carry = 0;
while (north !== null || south !== null || carry !== 0) {
let sum = carry;
if (north !== null) { sum += north.val; north = north.next; }
if (south !== null) { sum += south.val; south = south.next; }
tail.next = new ListNode(sum % 10, null);
tail = tail.next;
carry = Math.floor(sum / 10);
}
return dummy.next;
}def add_tallies(north, south):
dummy = ListNode(0)
tail = dummy
carry = 0
while north is not None or south is not None or carry:
total = carry
if north is not None:
total += north.val
north = north.next
if south is not None:
total += south.val
south = south.next
tail.next = ListNode(total % 10)
tail = tail.next
carry = total // 10
return dummy.nextRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.