VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sum-of-two-linked-lists/

⇱ Add Two Numbers represented as Linked List using Recursion - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Add Two Numbers represented as Linked List using Recursion

Last Updated : 23 Jul, 2025

Given two numbers represented as two lists, the task is to return the sum of two lists using recursion.

Note: There can be leading zeros in the input lists, but there should not be any leading zeros in the output list.

Examples:

Input: num1 = 4 -> 5, num2 = 3 -> 4 -> 5
Output: 3 -> 9 -> 0 
Explanation: Sum of 45 and 345 is 390.

👁 Add-two-numbers-represented-by-linked-lists-
Add two numbers represented as Linked List

Input: num1 = 0 -> 0 -> 6 -> 3, num2 = 0 -> 7
Output: 7 -> 0
Explanation: Sum of 63 and 7 is 70.

Input: num1 = 1 -> 2 -> 3, num2 = 9 -> 9 -> 9
Output: 1 -> 1 -> 2 -> 2 
Explanation: Sum of 123 and 999 is 1122.

Approach:

The idea is to use recursion to compute the sum. Reverse both the linked lists to start from the least significant digit. Now, traverse both the linked list recursively and in each recursive add the values of the current nodes and the carry from the previous step (initially, carry = 0). If there is a carry after the last nodes, append a new node with this carry. Finally, reverse the linked list to get the sum.


Output
1 1 2 2 

Time Complexity: O(m + n), where m and n are the sizes of given two linked lists.
Auxiliary Space: O(max(m, n))

Related article:

Comment