Add Two Numbers represented as Linked List using Recursion (original) (raw)
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 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.
C++ `
// C++ code to add two linked list using recursion
#include using namespace std;
class Node { public: int data; Node *next; Node(int val) { data = val; next = nullptr; } };
// Function to reverse a linked list Node *reverse(Node *head) { Node *prev = nullptr; Node *curr = head; Node *next = nullptr;
// Loop to reverse the linked list
while (curr != nullptr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;}
// Recursive function to add two numbers represented // by linked lists Node *addListRec(Node *num1, Node *num2, int &carry) {
// Base case: If both lists are empty and no carry is left
if (num1 == nullptr && num2 == nullptr && carry == 0) {
return nullptr;
}
int sum = carry;
// Add the value from the first list if it exists
if (num1 != nullptr) {
sum += num1->data;
num1 = num1->next;
}
// Add the value from the second list if it exists
if (num2 != nullptr) {
sum += num2->data;
num2 = num2->next;
}
carry = sum / 10;
Node *result = new Node(sum % 10);
// Recursively add remaining digits
result->next = addListRec(num1, num2, carry);
return result;}
// function to trim leading zeros in linked list Node* trimLeadingZeros(Node* head) { while (head != nullptr && head->data == 0) { head = head->next; } return head; }
// function for adding two linked lists Node *addTwoLists(Node *num1, Node *num2) { num1 = trimLeadingZeros(num1); num2 = trimLeadingZeros(num2);
// Reverse both lists to start addition from
// the least significant digit
num1 = reverse(num1);
num2 = reverse(num2);
int carry = 0;
Node *result = addListRec(num1, num2, carry);
// If there's any carry left after the addition,
// create a new node for it
if (carry != 0) {
Node *newNode = new Node(carry);
newNode->next = result;
result = newNode;
}
// Reverse the result list to restore
// the original order
return reverse(result);}
void printList(Node *head) { Node *curr = head; while (curr != nullptr) { cout << curr->data << " "; curr = curr->next; } cout << "\n"; }
int main() {
// Creating first linked list: 1 -> 2 -> 3
// (represents 123)
Node *num1 = new Node(1);
num1->next = new Node(2);
num1->next->next = new Node(3);
// Creating second linked list: 9 -> 9 -> 9
// (represents 999)
Node *num2 = new Node(9);
num2->next = new Node(9);
num2->next->next = new Node(9);
Node *sum = addTwoLists(num1, num2);
printList(sum);
return 0;}
C
// C code to add two linked list using recursion
#include <stdio.h>
struct Node { int data; struct Node *next; };
// Function to create a new node struct Node* createNode(int val) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = val; newNode->next = NULL; return newNode; }
// Function to reverse a linked list struct Node* reverse(struct Node* head) { struct Node* prev = NULL; struct Node* curr = head; struct Node* next = NULL;
// Loop to reverse the linked list
while (curr != NULL) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;}
// function to trim leading zeros in linked list struct Node* trimLeadingZeros(struct Node* head) { while (head->next != NULL && head->data == 0) head = head->next; return head; }
// Recursive function to add two numbers represented by linked lists struct Node* addListRec(struct Node* num1, struct Node* num2, int* carry) {
// Base case: If both lists are empty and no carry is left
if (num1 == NULL && num2 == NULL && *carry == 0) {
return NULL;
}
int sum = *carry;
// Add the value from the first list if it exists
if (num1 != NULL) {
sum += num1->data;
num1 = num1->next;
}
// Add the value from the second list if it exists
if (num2 != NULL) {
sum += num2->data;
num2 = num2->next;
}
*carry = sum / 10;
struct Node* result = createNode(sum % 10);
// Recursively add remaining digits
result->next = addListRec(num1, num2, carry);
return result;}
// Function for adding two linked lists struct Node* addTwoLists(struct Node* num1, struct Node* num2) { num1 = trimLeadingZeros(num1); num2 = trimLeadingZeros(num2);
// Reverse both lists to start addition from the
// least significant digit
num1 = reverse(num1);
num2 = reverse(num2);
int carry = 0;
struct Node* result = addListRec(num1, num2, &carry);
// If there's any carry left after the addition,
// create a new node for it
if (carry != 0) {
struct Node* newNode = createNode(carry);
newNode->next = result;
result = newNode;
}
// Reverse the result list to restore the original order
return reverse(result);}
// Function to print a linked list void printList(struct Node* head) { struct Node* curr = head; while (curr != NULL) { printf("%d ", curr->data); curr = curr->next; } printf("\n"); }
int main() {
// Creating first linked list: 1 -> 2 -> 3 (represents 123)
struct Node* num1 = createNode(1);
num1->next = createNode(2);
num1->next->next = createNode(3);
// Creating second linked list: 9 -> 9 -> 9 (represents 999)
struct Node* num2 = createNode(9);
num2->next = createNode(9);
num2->next->next = createNode(9);
struct Node* sum = addTwoLists(num1, num2);
printList(sum);
return 0;}
Java
// Java code to add two linked list using recursion
class Node { int data; Node next;
Node(int val) {
data = val;
next = null;
}
}class GfG {
// Function to reverse a linked list
static Node reverse(Node head) {
Node prev = null;
Node curr = head;
Node next = null;
// Loop to reverse the linked list
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to trim leading zeros in linked list
static Node trimLeadingZeros(Node head) {
while (head != null && head.data == 0) {
head = head.next;
}
return head;
}
// Recursive function to add two numbers
// represented by linked lists
static Node addListRec(Node num1, Node num2,
int[] carry) {
// Base case: If both lists are empty
// and no carry is left
if (num1 == null && num2 == null && carry[0] == 0) {
return null;
}
int sum = carry[0];
// Add the value from the first list if it exists
if (num1 != null) {
sum += num1.data;
num1 = num1.next;
}
// Add the value from the second list if it exists
if (num2 != null) {
sum += num2.data;
num2 = num2.next;
}
carry[0] = sum / 10;
Node result = new Node(sum % 10);
// Recursively add remaining digits
result.next = addListRec(num1, num2, carry);
return result;
}
// Function for adding two linked lists
static Node addTwoLists(Node num1, Node num2) {
num1 = trimLeadingZeros(num1);
num2 = trimLeadingZeros(num2);
// Reverse both lists to start addition from
// the least significant digit
num1 = reverse(num1);
num2 = reverse(num2);
// Array used to pass carry by reference
int[] carry = new int[1];
Node result = addListRec(num1, num2, carry);
// If there's any carry left after the addition,
// create a new node for it
if (carry[0] != 0) {
Node newNode = new Node(carry[0]);
newNode.next = result;
result = newNode;
}
// Reverse the list to restore the original order
return reverse(result);
}
// Function to print a linked list
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Creating first linked list:
// 1 -> 2 -> 3 (represents 123)
Node num1 = new Node(1);
num1.next = new Node(2);
num1.next.next = new Node(3);
// Creating second linked list:
// 9 -> 9 -> 9 (represents 999)
Node num2 = new Node(9);
num2.next = new Node(9);
num2.next.next = new Node(9);
Node sum = addTwoLists(num1, num2);
printList(sum);
}}
Python
Python code to add two linked lists using recursion
import sys class Node: def init(self, val): self.data = val self.next = None
Function to reverse a linked list
def reverse(head): prev = None curr = head next = None
# Loop to reverse the linked list
while curr is not None:
next = curr.next
curr.next = prev
prev = curr
curr = next
return prevfunction to trim leading zeros from linked list
def trimLeadingZeros(head): while head and head.data == 0: head = head.next return head
Recursive function to add two numbers represented
by linked lists
def addListRec(num1, num2, carry):
# Base case: If both lists are empty and no carry is left
if num1 is None and num2 is None and carry[0] == 0:
return None
sum = carry[0]
# Add the value from the first list if it exists
if num1 is not None:
sum += num1.data
num1 = num1.next
# Add the value from the second list if it exists
if num2 is not None:
sum += num2.data
num2 = num2.next
carry[0] = sum // 10
result = Node(sum % 10)
# Recursively add remaining digits
result.next = addListRec(num1, num2, carry)
return resultFunction for adding two linked lists
def addTwoLists(num1, num2): num1 = trimLeadingZeros(num1) num2 = trimLeadingZeros(num2)
# Reverse both lists to start addition from the
# least significant digit
num1 = reverse(num1)
num2 = reverse(num2)
carry = [0]
result = addListRec(num1, num2, carry)
# If there's any carry left after the addition,
# create a new node for it
if carry[0] != 0:
newNode = Node(carry[0])
newNode.next = result
result = newNode
# Reverse the result list to restore the original order
return reverse(result)def printList(head): curr = head while curr is not None: print(curr.data, end=' ') curr = curr.next print()
if name == "main":
# Creating first linked list:
# 1 -> 2 -> 3 (represents 123)
num1 = Node(1)
num1.next = Node(2)
num1.next.next = Node(3)
# Creating second linked list:
# 9 -> 9 -> 9 (represents 999)
num2 = Node(9)
num2.next = Node(9)
num2.next.next = Node(9)
sumList = addTwoLists(num1, num2)
printList(sumList)C#
// C# code to add two linked list using recursion
using System;
class Node { public int data; public Node next;
public Node(int val) {
data = val;
next = null;
}}
// Function to reverse a linked list class GfG { static Node Reverse(Node head) { Node prev = null; Node curr = head; Node next = null;
// Loop to reverse the linked list
while (curr != null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to trim leading zeros from linked list
static Node TrimLeadingZeros(Node head) {
while (head != null && head.data == 0) {
head = head.next;
}
return head;
}
// Recursive function to add two numbers represented
// by linked lists
static Node AddListRec(Node num1, Node num2,
ref int carry) {
num1 = TrimLeadingZeros(num1);
num2 = TrimLeadingZeros(num2);
// Base case: If both lists are empty and
// no carry is left
if (num1 == null && num2 == null && carry == 0) {
return null;
}
int sum = carry;
// Add the value from the first list if it exists
if (num1 != null) {
sum += num1.data;
num1 = num1.next;
}
// Add the value from the second list if it exists
if (num2 != null) {
sum += num2.data;
num2 = num2.next;
}
carry = sum / 10;
Node result = new Node(sum % 10);
// Recursively add remaining digits
result.next = AddListRec(num1, num2, ref carry);
return result;
}
// Function for adding two linked lists
static Node AddTwoLists(Node num1, Node num2) {
// Reverse both lists to start addition from
// the least significant digit
num1 = Reverse(num1);
num2 = Reverse(num2);
int carry = 0;
Node result = AddListRec(num1, num2, ref carry);
// If there's any carry left after the addition,
// create a new node for it
if (carry != 0) {
Node newNode = new Node(carry);
newNode.next = result;
result = newNode;
}
// Reverse the result list to restore
// the original order
return Reverse(result);
}
static void PrintList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main() {
// Creating first linked list: 1 -> 2 -> 3
// (represents 123)
Node num1 = new Node(1);
num1.next = new Node(2);
num1.next.next = new Node(3);
// Creating second linked list: 9 -> 9 -> 9
// (represents 999)
Node num2 = new Node(9);
num2.next = new Node(9);
num2.next.next = new Node(9);
Node sum = AddTwoLists(num1, num2);
PrintList(sum);
}}
JavaScript
// Javascript code to add two linked list using recursion
class Node { constructor(val) { this.data = val; this.next = null; } }
// Function to reverse a linked list function reverse(head) { let prev = null; let curr = head; let next = null;
// Loop to reverse the linked list
while (curr !== null) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;}
function trimLeadingZeros(head) { while (head !== null && head.data === 0) { head = head.next; } return head; }
// Recursive function to add two numbers represented // by linked lists function addListRec(num1, num2, carry) {
// Base case: If both lists are empty and no carry is left
if (num1 === null && num2 === null && carry === 0) {
return null;
}
let sum = carry;
// Add the value from the first list if it exists
if (num1 !== null) {
sum += num1.data;
num1 = num1.next;
}
// Add the value from the second list if it exists
if (num2 !== null) {
sum += num2.data;
num2 = num2.next;
}
carry = Math.floor(sum / 10);
let result = new Node(sum % 10);
// Recursively add remaining digits
result.next = addListRec(num1, num2, carry);
return result;}
// Function for adding two linked lists function addTwoLists(num1, num2) { num1 = trimLeadingZeros(num1); num2 = trimLeadingZeros(num2);
// Reverse both lists to start addition from
// the least significant digit
num1 = reverse(num1);
num2 = reverse(num2);
let carry = 0;
let result = addListRec(num1, num2, carry);
// If there's any carry left after the addition,
// create a new node for it
if (carry !== 0) {
let newNode = new Node(carry);
newNode.next = result;
result = newNode;
}
// Reverse the result list to restore
// the original order
return reverse(result);}
function printList(head) { let curr = head; let result = ''; while (curr !== null) { result += curr.data + ' '; curr = curr.next; } console.log(result.trim()); }
// Creating first linked list: 1 -> 2 -> 3 // (represents 123) let num1 = new Node(1); num1.next = new Node(2); num1.next.next = new Node(3);
// Creating second linked list: 9 -> 9 -> 9 // (represents 999) let num2 = new Node(9); num2.next = new Node(9); num2.next.next = new Node(9);
let sum = addTwoLists(num1, num2); printList(sum);
`
**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:
