Quick revision
The short answer
Adding two numbers stored as linked lists of digits, units digit first, is a linked list problem solved by walking both lists in step. We add the two digits and a carry, write sum % 10 into a new node, keep sum / 10 as the carry, and continue while either list has digits or a carry remains. It takes O(max(m, n)) time and works for numbers of any length.
The solution
What it costs
| Approach | Time | Works for any length? |
|---|---|---|
| Convert each list to a long and back | O(m + n) | no, overflows past 18 digits |
Convert to BigInteger and back | O(m + n) plus conversions | yes, but hides the work |
| Add digit by digit with a carry | O(max(m, n)) | yes |
What to remember
We added two linked-list numbers by doing school column addition while walking both lists in step, writing sum % 10 at the tail, keeping sum / 10 as the carry, and going on while either list has digits or a carry remains. It takes O(max(m, n)) time and O(1) extra space besides the answer. The clue to remember is two linked lists combined position by position: walk them together, let a finished list count as 0, and carry state forward.
1What it means to add two numbers kept as chains of digits
Think back to adding large numbers at school. You wrote one number under the other, started with the units column on the right, added the two digits and wrote down the last digit of the answer. If the answer was ten or more, you wrote a small 1 above the next column, called the carry, and moved one column left. This whole article is that school method, done on a linked list instead of on paper.
A linked list, if it is new to you, is a chain of small boxes called nodes. Each node holds one value, here a single digit from 0 to 9, and one reference called next, which says which node comes after it. The last node's next is null, Java's word for nothing. We are given two such chains, each one a number, and we must build a third chain holding their sum.
There is one surprise in the way the numbers are stored. The digits are kept backwards, units digit first. So the chain 2 -> 4 -> 3 means the number 342, because 2 is the units digit, 4 the tens digit and 3 the hundreds digit. In the same way 5 -> 6 -> 4 means 465. Their sum is 807, and we must return it in the same backwards style, as 7 -> 0 -> 8.
Let us add them by hand, exactly as at school. Units column, 2 + 5 is 7, so we write 7 and carry nothing. Tens column, 4 + 6 is 10, so we write 0 and carry 1. Hundreds column, 3 + 4 plus the carried 1 is 8, so we write 8. Reading what we wrote in the order we wrote it gives 7, 0, 8, which is exactly the chain we must return. Notice that the backwards storage, which looked strange, hands us the digits in the very order school addition wants them.
Row one is 342 stored units first, row two is 465, and row three is the list we must build, 807 stored units first.
- first
- 342
- second
- 465
- sum
- 807
1The top two chains are 342 and 465 with the units digit first. The bottom chain is their sum, 807, stored the same way as 7 -> 0 -> 8.
What to assume
- Are the digits stored units first?
- We assume so.
- Can the lists have different lengths, as in 999 + 1?
- Yes.
- Leading zeros?
- No, apart from the number 0 itself.
- May we change the inputs?
- No, so we build a new list.
- How long can the numbers be?
- Hundreds of digits, far more than any Java number type holds.
The problem is smaller than it looks. If you can add 342 and 465 on paper, you already know the algorithm; the only new work is walking two chains at once and remembering one small number, the carry, between steps.
A careful candidate asks a few questions first. Are the digits stored units first? We assume so; the FAQs cover the other order. Can the lists have different lengths, as in 999 + 1? Yes. Leading zeros? No, apart from the number 0 itself. May we change the inputs? No, so we build a new list. How long can the numbers be? Hundreds of digits, far more than any Java number type holds, and that answer will matter soon.
Your turn
Solve it before you read on. Above is one example with the result expected for it. Under What to assume are the points this solution takes as given. Say your approach aloud or type it below, and write the code in your own editor. Then take one hint at a time and compare.
2How to recognise that this is a linked list walking problem
When a beginner reads this question for the first time, the word numbers takes over. Java already knows how to add numbers, so the job must be to get the numbers out, add them, and put the answer back. That is a reasonable first thought, and it is our brute force in the next section. But let us read the question again, slowly.
The first clue is the phrase singly linked list. Such a list can only be walked forwards, one node at a time, so we should ask which order of work suits a forward walk.
The second clue is the order of the digits. The units digit comes first, and school addition starts with the units column, so walking forward through both lists is the same as moving leftwards through the columns on paper. When the storage order matches the order the work needs, the work can be done during a single walk.
The third clue is the word two, with an answer that is a list too. We combine two lists position by position into a new one, the same shape as merging two sorted lists: one reference on each list, both moving forward in one loop, output built as we go.
Put together, the clues point to the linked lists family, and specifically to walking two lists in step. In step means the two references move forward together, one node each per round, like two people walking side by side. The general rule to carry away is this. When two linked lists must be combined position by position, keep one reference per list, treat a list that has ended as giving a neutral value, here 0, carry any state from one position to the next, and append each result to a new list.
Some patterns look close but do not fit. Fast and slow pointers walk one list at two speeds to find a middle or a loop, and nothing here needs a middle. Reversing a list is needed only when the most significant digit comes first, which the FAQs come back to.
3The honest first attempt, turning the lists into numbers
When something feels unfamiliar, a good first step is to turn it into something we already know. We know how to add two Java numbers with a plus sign. So the honest first attempt reads each list into a long, adds the two longs, and then turns the sum back into a list. Trying this is a good thing, because it is quick to write and it works on the example.
Let us do it by hand on 2 -> 4 -> 3. A variable place, the value of the current column, starts at 1. The nodes add 2 times 1, 4 times 10 and 3 times 100, so x becomes 342, and the same walk gives y = 465. The sum is 807. To turn it back into a list we take 807 % 10, the remainder after dividing by ten, which is 7. Then 807 / 10 is 80 in whole-number division, and repeating gives 0 and then 8. The list is 7 -> 0 -> 8, which is correct.
Now let us count. The reading walks visit 3 + 3 = 6 nodes and the answer takes 3 more steps, about 9 in all. For lists of m and n digits that is about m + n plus the answer, so the time is O(m + n), read as order m plus n, meaning the work grows in step with the number of digits. So this attempt is not slow at all.
Then where is the problem? It is correctness. A long holds numbers only up to about 9.2 times 10 to the power 18, a 19-digit number, and our lists may have hundreds of digits. Add 1 to twenty 9s and place overflows, which means it grows too big for a long and silently wraps around to a wrong value. Java gives no error; it simply returns a wrong list.
BigInteger, Java's class for numbers of any size, would fix the overflow, but it only hides the same column addition inside a library, and it still builds a whole number only to take it apart again.
So let us ask the question that leads further. When we walked the lists to build x and y, we stood on the units digits first, then the tens digits, then the hundreds. That is exactly the order in which school addition wants them. Did we need to build the whole numbers at all, or could we have added the digits while we stood on them?
4Getting to digit-by-digit addition, one observation at a time
The first observation is that we can add while we walk. A reference a starts on the first node of the first list and b on the first node of the second, both on units digits, 2 and 5. We add them, write the answer into a new node, and move a and b forward onto the tens digits. No whole number is ever built, so nothing can overflow.
The second observation is about the carry. When a column adds up to ten or more, only the last digit is written. In Java, sum % 10 gives the digit and sum / 10 gives the carry, so for the tens column, sum 10, we write 0 and carry 1. How big can the carry get? The largest column is 9 + 9 + 1 = 19, so it is always 0 or 1.
The third observation handles different lengths. In 999 + 1 the second list ends after one node. On paper we treat the empty places as 0, and in code we do the same, so if b is null it adds nothing to sum, and one loop covers every case.
Adding 999 and 1. After the third column both lists are finished, yet a carry of 1 remains and needs one more node.
- carry
- 0
1We add 999 and 1, stored as 9 -> 9 -> 9 and 1. The second list is much shorter than the first.
- sum
- 10
- carry
- 1
- b
- null, counts as 0
2Units column, 9 + 1 + 0 is 10. We write 0 and carry 1. b is now null, because its list had only one digit.
- sum
- 10
- carry
- 1
3Tens column, 9 + 0 + 1 is 10 again. A finished list simply adds 0, so we write 0 and still carry 1.
- sum
- 10
- carry
- 1
- a and b
- both null
4Hundreds column, 9 + 0 + 1 is 10 once more. We write 0, carry 1, and now a is null as well.
- sum
- 1
- carry
- 0
- answer
- 1000
5Both lists are finished but carry is 1, so the loop runs once more and writes 1. The answer 0 -> 0 -> 0 -> 1 is 1000.
Now comes the question every beginner asks. When should the loop stop? The natural answer is when both lists are finished, so the condition would be a != null || b != null. Let us try it on 999 + 1 and see what we lose. The picture above shows the run. After the hundreds column, both a and b are null, but the carry is still 1. If the loop stopped there, the answer would be 0 -> 0 -> 0, which is 0, not 1000. On paper we would never forget the last carried 1; we would write it at the front. So the loop must also continue while carry is not 0, and the full condition is a != null || b != null || carry != 0.
The last observation is about building the answer. Each digit goes on the end, so a reference tail stays on the last node created. At the start there is no last node, so we create a dummy node, a spare node that is not part of the answer. tail starts on it, every digit is added with the same two lines, and at the end we return dummy.next, the first real digit.
Why is this always correct? At the top of every round, the answer holds the lowest digits of the true sum, and carry holds what those columns pushed onwards. Each round adds one column correctly, as on paper, and when nothing is left, the answer is complete.
5Watching the digits add up for 342 + 465
The picture plays the loop on our example, using the values the Java method produces. The top row is the first number, the middle row the second, and the bottom row the answer, starting with the faded dummy node. Under the rows are the references a, b and tail.
Each column takes two frames, one for adding the digits and the carry, and one for writing the new node and stepping forward. Watch the carry in the tens column.
Top row 342, middle row 465, bottom row the answer being built after a faded dummy node. Each column takes two frames, adding then writing.
- carry
- 0
- result
- empty
1We begin with a on 2, b on 5, carry 0, and a result that holds only the dummy node, with tail resting on it.
- column
- units
- carry in
- 0
- sum
- 7
2In the units column we add the carry 0 to the digits 2 and 5, so sum is 7.
- column
- units
- digit written
- 7
- carry out
- 0
3We write 7 % 10 = 7 into a new node after the dummy, carry becomes 7 / 10 = 0, and a, b and tail all step forward.
- column
- tens
- carry in
- 0
- sum
- 10
4In the tens column we add the carry 0 to 4 and 6, so sum is 10, too big for one digit.
- column
- tens
- digit written
- 0
- carry out
- 1
5We write 10 % 10 = 0 into a new node and keep carry = 10 / 10 = 1 for the next column, just as we carry a 1 on paper.
- column
- hundreds
- carry in
- 1
- sum
- 8
6In the hundreds column we add the carry 1 to 3 and 4, so sum is 8.
- column
- hundreds
- digit written
- 8
- carry out
- 0
7We write 8 and carry becomes 0. Both a and b have now stepped off the ends of their lists and are null.
- carry
- 0
- answer
- 7 -> 0 -> 8
- as a number
- 807
8a and b are null and carry is 0, so the loop stops. We return
dummy.next, the list 7 -> 0 -> 8, which is 807.
Let us read the important frames back. In the units column, sum is 7, and the 7 goes into the node after the dummy. In the tens column sum is 10, the frame that matters most: only the 0 is written, and the 1 is held in carry, like the small 1 written above the next column at school.
In the hundreds column the carried 1 joins 3 and 4 to make 8. Then a and b are both null and carry is 0, so all three parts of the loop condition are false and the loop ends.
In the last frame tail is on the 8, but the answer starts after the dummy, so we return dummy.next.
6The complete Java program
Here is the full program. It has a small Node class, the add method, and two helpers, of to build a list from digits and show to print one. main runs our example, 999 + 1, 0 + 0, and 5 + 5, and each comment shows what that line prints.
Let us read add in the order it runs. The first line creates the dummy node and puts tail on it, and carry starts at 0, because nothing is carried into the units column.
The while condition keeps going while either list has digits or a carry is waiting. Leaving out the carry part is the mistake beginners make most often, because it is tempting to think the work ends with the input.
Inside the loop sum starts at carry. If a is not null, we add its digit and move a forward in the same line, and the same for b. A null list adds nothing, which is how different lengths are handled without extra code.
Next, tail.next = new Node(sum % 10) hangs the new digit on the end of the answer, and tail = tail.next moves tail onto it, ready for the next round. Finally carry = sum / 10 keeps the carry for the next column. When the loop ends we return dummy.next, skipping the dummy.
So main prints 7 -> 0 -> 8, then 0 -> 0 -> 0 -> 1 for 999 + 1, then 0, and 0 -> 1 for 5 + 5.
7How much it costs, and the cases at the edges
Let us count on the example first. The loop ran three times, once per column, creating one node each time; for 999 + 1 it ran four times because of the final carry. For lists of m and n digits it runs max(m, n) times, or one more, so the time is O(max(m, n)), and every digit must be read at least once anyway.
The answer list needs max(m, n) or max(m, n) + 1 new nodes, which is the output itself. Beyond the output we use only a, b, tail, sum and carry, so the extra space is O(1), read as constant. The input lists are only read, never changed.
| Approach | Time | Works for any length? |
|---|---|---|
| Convert each list to a long and back | O(m + n) | no, overflows past 18 digits |
Convert to BigInteger and back | O(m + n) plus conversions | yes, but hides the work |
| Add digit by digit with a carry | O(max(m, n)) | yes |
The edge cases need no special code. Lists of different lengths work because a null list adds 0. A carry out of the last column works because of the carry part of the loop condition. Two lists holding just 0 give a single 0 node, as main shows. If a caller passes null for one list, the same loop treats it as the number 0 and returns a copy of the other list.
A good case to trace by hand in an interview is 5 + 5. The loop writes 0 and carries 1, then runs again for the carry alone and writes 1, giving 0 -> 1, which is 10.
8Follow-up questions
What if the digits are stored with the most significant digit first?
Then 342 is stored as 3 -> 4 -> 2, and the units digits sit at the ends, which a singly linked list cannot reach first. One way is to push each list's digits onto a stack, which works like a pile of plates, last on and first off. Popping both stacks gives the digits from the units column upwards, and we put each new node at the front of the answer, so it comes out most significant first. Another way is to reverse both lists, add as before, and reverse the answer. Both take O(m + n) time, and the stack version leaves the inputs untouched.
Why use a dummy node instead of handling the first digit separately?
Without a dummy, the first digit is special, because there is no tail yet to hang it on, so we would need extra code before the loop or an if inside it, both places where bugs hide. The dummy gives every digit the same two lines and costs one extra node, and returning dummy.next means the caller never sees it.
Could you write it recursively?
Yes. A helper add(a, b, carry) returns null when both lists are null and the carry is 0. Otherwise it makes a node holding sum % 10, sets its next to the helper called on the following nodes with carry sum / 10, and returns it. Each digit adds one waiting method call, so it uses O(max(m, n)) stack space and can overflow on very long numbers. The loop is the safer default.
9Conclusion
We added two linked-list numbers by doing school column addition while walking both lists in step, writing sum % 10 at the tail, keeping sum / 10 as the carry, and going on while either list has digits or a carry remains. It takes O(max(m, n)) time and O(1) extra space besides the answer. The clue to remember is two linked lists combined position by position: walk them together, let a finished list count as 0, and carry state forward.
Open with
Let me confirm the format first. Each node holds one digit, the units digit comes first, the lists can have different lengths, and I should return a new list in the same format without changing the inputs.
Then, if they want more
Converting each list to a long and adding is quick, but a long overflows past about 19 digits and these numbers can be much longer, so it would silently return wrong answers. Since the digits are stored units first, a forward walk visits them in exactly the order of column addition, so I will add digit by digit.
I keep a reference on each list, a carry starting at 0, and a dummy node with a tail reference for the answer. In each round, sum is the carry plus whichever digits exist, a missing digit counting as 0. I append a node with sum % 10, set carry to sum / 10, and move the references on.
The loop runs while either list has digits or the carry is not 0, which is what turns 999 + 1 into 0, 0, 0, 1. I return dummy.next. That is O(max(m, n)) time and O(1) extra space besides the output.
About this answer
- Last updated
- 15 November 2026
- Written for
- Java 21, using only java.util
- Checked
- The Java solution was compiled and run, and each printed result matches the comment beside it. The pictures are checked against the format when the site is built. The explanation around the code is a study note: confirm anything critical against the documentation below. What the checks cover
- Where the question comes from
- Public interview write-ups, reworded by Scrivio; counted 42 times in that collection. How it is counted
- Official documentation
- StringBuilder (Java 21), java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Add two numbers stored as singly linked lists of digits.": https://scrivio-interview-library.netlify.app/data-structures/solutions/add-two-numbers-stored-as-singly-linked-lists-of-digits-de8b70/