Add two big numbers represented as linked lists without reversing the linked lists
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When a number is stored as a linked list in forward order, the head contains the most significant digit. That makes addition awkward because elementary-school addition runs from right to left, starting at the least significant digit.
You can still add two such numbers without reversing either list. The usual solutions are recursion with padding or an explicit stack, both of which let you process digits from the tail back toward the head.
Problem Shape
Suppose the lists represent 7243 and 89:
- '
7 -> 2 -> 4 -> 3' - '
8 -> 9'
The expected answer is 7332, stored as:
- '
7 -> 3 -> 3 -> 2'
The constraint is that you should not reverse the input lists in place. That usually means either:
- pad the shorter list and recurse to the end
- push digits onto stacks and pop from the back
A Recursive Solution With Padding
The cleanest interview solution is to equalize the lengths first. Then you recurse until both pointers reach the tail, compute the digit sum while the call stack unwinds, and propagate carry back toward the front.
This prints [7, 3, 3, 2].
Why Recursion Works
The recursive call moves both pointers all the way to the last digit before doing any addition. That effectively simulates “starting from the right” without mutating the lists.
Padding is important because it lines up corresponding place values. Without padding, 7 and 8 would be incorrectly treated as the same digit position in the example above.
The helper returns two pieces of information:
- the carry for the next more-significant position
- the partially built result list
That makes the algorithm easy to reason about and easy to test.
Time and Space Complexity
Let n be the length of the longer list.
- Time complexity is
O(n)because each node is visited a constant number of times. - Extra space is
O(n)if you count recursion stack frames.
If recursion depth is a concern, the stack-based approach gives the same O(n) time without modifying the input lists.
A Stack-Based Alternative
The stack version is often more practical in languages where recursion depth is limited.
This also avoids reversing the lists. It simply moves the digits into LIFO structures first.
Common Pitfalls
A common bug is forgetting to pad the shorter list in the recursive version. That misaligns digits and gives wrong sums.
Another bug is dropping the final carry. If 999 + 1 produces 000 instead of 1000, the carry handling at the front is missing.
People also accidentally mutate the original lists while building the result. Unless the problem explicitly allows reuse, return a new list.
Finally, watch base cases carefully. Recursive list code often fails on empty input because the termination condition is too loose or too strict.
Summary
- Forward-order linked lists can be added without reversing them.
- The standard recursive solution pads the shorter list, recurses to the tail, and propagates carry backward.
- The stack-based solution is an iterative alternative with the same time complexity.
- Both approaches run in
O(n)time. - The most common mistakes are digit misalignment and forgetting the final carry node.

