Today, I want to tackle a simpler problem of adding two numbers represented as linked lists. Each node contains a digit between 0 and 9. This problem will use some of the techniques we used in the merging of k sorted lists. Even though this problem is quite simple to solve, it helps build intuition on solving linked list problems.

Approach

The only main gotcha in this problem is we need to keep track of carry from each sum till we reach the end of the lists and add the left over carry to the result list. Otherwise, things are pretty straightforward. Notice how we keep track of previous node and head node exactly like the merging of the k sorted lists problem.

Alternate approach

I have another approach which is extremely similar to merge operation in mergesort. This approach is slightly more readable but in no means more efficient.