Add Two Numbers Represented By Linked Lists

less than 1 minute read

I found the problem at this link.

I have solved a similar problem from leetcode, but the digits in the linked lists were backwards, so it was easier since I can do regular addition starting at the beginning of the lists.

For this one, there are two approach:

  • Find two pointers to end of two lists and continue like the old problem
  • Recursively add sum of next nodes to sum of currents node until we reach ends of lists which are null
  • Since method 1 is easy, we can have look at method 2

    Accroding to geeksforgeeks, we need to watch out for the case that one link list is longer than the other. In that case, we will move the shorter list's last node to the position of the longer list's last node. Perform addition on two linked lists for the length of the shorter list. After that, we add the remaining digits in the longer lists to our result.

    Updated: