JavaScript Program To Add Two Numbers Represented By Linked Lists- Set 1

Adding two numbers is an easy task but could be tricky if the numbers are given in the form of the linked list. Each node of the linked list contains the digit of the number it represents in a continuous manner from the first node to the last node. We will be given two linked lists representing two different numbers and we have to add them and return the third number in the form of a linked list.

Problem Statement

Given two linked lists where each node contains a single digit, we need to add the numbers they represent and return the sum as a new linked list.

Input

1 -> 2 -> 3 -> null  (represents 123)
3 -> 2 -> 4 -> null  (represents 324)

Output

4 -> 4 -> 7 -> null  (represents 447)

Explanation: The first number is 123, the second number is 324, and their sum is 447, returned as a linked list.

Method 1: Converting to Number Approach

In this approach, we first convert the linked lists to integers, perform addition, then convert the result back to a linked list.

// Class to create the structure of the nodes 
class Node {
   constructor(data) {
      this.value = data;
      this.next = null;
   }
}

// Function to print the linked list
function print(head) {
   let temp = head;
   let ans = "";
   while (temp.next != null) {
      ans += temp.value;
      ans += " -> ";
      temp = temp.next;
   }
   ans += temp.value;
   ans += " -> null";
   console.log(ans);
}

// Function to add data in linked list 
function add(data, head, tail) {
   return tail.next = new Node(data);
}

// Function to convert linked list to number 
function LL_to_int(head) {
   let temp = "";
   let cur = head;
   while (cur != null) {
      temp += cur.value.toString();
      cur = cur.next;
   }
   return parseInt(temp);
}

// Function to convert number to linked list
function num_to_LL(num) {
   let str = num.toString();
   let head = new Node(parseInt(str[0]));
   let tail = head;
   for (let i = 1; i < str.length; i++) {
      tail = add(parseInt(str[i]), head, tail);
   }
   
   console.log("The final answer is: ");
   print(head);
}

// Defining first number
let num1 = new Node(1);
let tail = num1;
tail = add(2, num1, tail);
tail = add(3, num1, tail);
console.log("The given first number is: ");
print(num1);

// Defining second number
let num2 = new Node(3);
tail = num2;
tail = add(2, num2, tail);
tail = add(4, num2, tail);
console.log("The given second number is: ");
print(num2);

// Converting both linked lists to integers and adding them
let int_num1 = LL_to_int(num1);
let int_num2 = LL_to_int(num2);
let ans = int_num1 + int_num2;

// Converting result back to linked list 
num_to_LL(ans);
The given first number is: 
1 -> 2 -> 3 -> null
The given second number is: 
3 -> 2 -> 4 -> null
The final answer is: 
4 -> 4 -> 7 -> null

Method 2: Reverse Linked List Approach

This approach processes the numbers digit by digit from right to left (least significant digit first) by reversing the representation or starting with reversed linked lists.

// Class to create the structure of the nodes 
class Node {
   constructor(data) {
      this.value = data;
      this.next = null;
   }
}

// Function to print the linked list
function print(head) {
   let temp = head;
   let ans = "";
   while (temp.next != null) {
      ans += temp.value;
      ans += " -> ";
      temp = temp.next;
   }
   ans += temp.value;
   ans += " -> null";
   console.log(ans);
}

// Function to add data in linked list 
function add(data, head, tail) {
   return tail.next = new Node(data);
}

// Function to convert string to linked list (reversed order)
function num_to_LL(str) {
   let head = new Node(parseInt(str[str.length - 1]));
   let tail = head;
   for (let i = str.length - 2; i >= 0; i--) {
      tail = add(parseInt(str[i]), head, tail);
   }
   
   console.log("The final answer is: ");
   print(head);
}

// Function to add values of the linked lists
function addLL(ll1, ll2) {
   let str = "";
   let carry = 0;
   
   while ((ll1 != null) || (ll2 != null) || carry != 0) {
      let sum = carry;
      
      if (ll1 != null) {
         sum += ll1.value;
         ll1 = ll1.next;
      }
      
      if (ll2 != null) {
         sum += ll2.value;
         ll2 = ll2.next;
      }
      
      str += (sum % 10).toString();
      carry = Math.floor(sum / 10);
   }
   
   // Calling function to print the answer
   num_to_LL(str);
}

// Defining first number in reverse manner (3->2->1 represents 123)
let num1 = new Node(3);
let tail1 = num1;
tail1 = add(2, num1, tail1);
tail1 = add(1, num1, tail1);
console.log("The given first number in reverse manner is: ");
print(num1);

// Defining second number in reverse manner (4->2->3 represents 324)
let num2 = new Node(4);
let tail2 = num2;
tail2 = add(2, num2, tail2);
tail2 = add(3, num2, tail2);
console.log("The given second number in reverse manner is: ");
print(num2);

// Calling the add function 
addLL(num1, num2);
The given first number in reverse manner is: 
3 -> 2 -> 1 -> null
The given second number in reverse manner is: 
4 -> 2 -> 3 -> null
The final answer is: 
7 -> 4 -> 4 -> null

Comparison

Method Time Complexity Space Complexity Handles Large Numbers
Convert to Number O(M + N) O(max(M, N)) Limited by integer size
Reverse Approach O(M + N) O(max(M, N)) Yes, handles any size

Key Points

  • Method 1 is simpler but limited by JavaScript's number precision
  • Method 2 handles arbitrarily large numbers by processing digits individually
  • Both methods have similar time and space complexity
  • Proper carry handling is crucial in digit-by-digit addition

Conclusion

We implemented two approaches to add numbers represented as linked lists. The reverse approach is more robust for large numbers, while the conversion approach is conceptually simpler for smaller numbers.

Updated on: 2026-03-15T23:19:01+05:30

331 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements