C1 Final | Similar to "Add Two Numbers II"
Anonymous User
878

Was asked this question. It is similar to Add Two Numbers II but has differences which I will explain.

Problem: You are given three possibly very large integers stored as Linked Lists in decreasing orders of magnitude, where each group of up to three integers represents 3 orders of magnitude. Leading zeros are excluded. The goal is to return the average of the integers, as a Linked List. It can be assumed each Linked List has the same number of nodes, and the final answer will be a whole number. You cannot convert the Linked Lists to integers, as the solution would be trivial (assume we are on a system that doesn't support large integers, which is why we are using linkedlists in the first place.).

Example 1:
10
20
30
Answer:
20

Example 2:
1 -> 0 (1,000)
5 -> 0 (5,000)
75 -> 500 (75,500)
Answer:
27 -> 0 (27,000)

Example 3:
995 -> 581 -> 458
401 -> 163 -> 916
856 -> 494 -> 113
Answer:
751 -> 79 -> 829

Example 3:
55 -> 0 -> 1 (55,000,001)
999 -> 9 -> 814 (999,009,814)
3 -> 600 -> 0 (3,600,000)
Answer:
352 -> 536 -> 605

I'm pretty sure this has to be solved by stepping through each order of magnitude for the 3 linked lists at a time and building the solution, but can't figure out the exact formula for when carrying is necessary, and why it's done. I feel like it's something like taking the decimal remainder of the division from one magnitude, multiplying it by 100, and then adding that to the average for the next node, but I don't understand why that seems to work.

Comments (4)