Add two numbers represented by Linked List without any extra space
Last Updated :
11 Jan, 2023
Given two numbers represented by two linked lists, write a function that returns sum list. The sum list is linked list representation of addition of two input numbers. Expected Space Complexity O(1).
Examples:
Input:
L1 = 5 -> 6 -> 3 -> NULL
L2 = 8 -> 4 -> 2 -> NULL
Output: 1 -> 4 -> 0 -> 5 -> NULL
Input:
L1 = 1 -> 0 -> 0 -> NULL
L2 = 9 -> 1 -> NULL
Output: 1 -> 9 -> 1 -> NULL
Approach: We have discussed a solution here where we used recursion to reach to the least significant number in the lists, but due to the involvement of the stack, the space complexity of the solution becomes O(N)
Here the target is to do the sum inplace, and return the modified sum list.
The idea is to first reverse both the linked list, so new head of the list points to least significant number and we can start adding as described here and instead of creating a new list, we modify the existing one and return the head of modified list.
Following are the steps:
- Reverse List L1.
- Reverse List L2.
- Add the nodes of both the lists iteratively.
- Reverse the resultant list and return its head.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
class LinkedList;
class Node {
int data;
Node* next;
friend LinkedList;
public :
Node();
Node( int x);
};
Node::Node()
{
data = 0;
next = NULL;
}
Node::Node( int x)
{
data = x;
next = NULL;
}
class LinkedList {
public :
Node* head;
LinkedList();
void insert( int x);
void reverse();
void traverse();
void sum(LinkedList*);
};
LinkedList::LinkedList()
{
head = NULL;
}
void LinkedList::insert( int x)
{
Node* node = new Node();
node->data = x;
if (head == NULL)
head = node;
else {
node->next = head;
head = node;
}
}
void LinkedList::reverse()
{
Node *prev = NULL, *curr = head;
while (curr) {
Node* temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
}
head = prev;
}
void LinkedList::traverse()
{
Node* temp = head;
while (temp) {
cout << temp->data << " -> " ;
temp = temp->next;
}
cout << "NULL" ;
}
void LinkedList::sum(LinkedList* l2)
{
reverse();
l2->reverse();
Node *start1 = head, *start2 = l2->head;
Node* prev = NULL;
int carry = 0;
while (start1 && start2) {
int temp = start1->data + start2->data + carry;
start1->data = temp % 10;
carry = temp / 10;
prev = start1;
start1 = start1->next;
start2 = start2->next;
}
if (start1 || start2) {
if (start2)
prev->next = start2;
start1 = prev->next;
while (start1) {
int temp = start1->data + carry;
start1->data = temp % 10;
carry = temp / 10;
prev = start1;
start1 = start1->next;
}
}
if (carry > 0) {
prev->next = new Node(carry);
}
reverse();
}
int main()
{
LinkedList* l1 = new LinkedList();
l1->insert(3);
l1->insert(6);
l1->insert(5);
LinkedList* l2 = new LinkedList();
l2->insert(2);
l2->insert(4);
l2->insert(8);
l1->sum(l2);
l1->traverse();
return 0;
}
|
Java
import java.util.*;
class Node
{
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
class LinkedList
{
Node head;
void traverse(Node head)
{
while (head != null )
{
System.out.print(head.data + "->" );
head = head.next;
}
}
void insert( int x)
{
Node temp = new Node(x);
if (head == null ) head = temp;
else
{
temp.next = head;
head = temp;
}
}
public static Node reverse(Node head)
{
if (head == null || head.next == null ) return head;
Node prev = null ;
Node curr = head;
while (curr != null )
{
Node temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
head = prev;
return head;
}
public static Node sum(Node l1, Node l2)
{
if (l2 == null ) return l1;
if (l1 == null ) return l2;
l1 = reverse(l1);
l2 = reverse(l2);
Node head = l1;
Node prev = null ;
int c = 0 ,sum;
while (l1 != null && l2 != null )
{
sum = c + l1.data + l2.data;
l1.data = sum % 10 ;
c = sum / 10 ;
prev = l1;
l1 = l1.next;
l2 = l2.next;
}
if (l1 != null ||l2 != null )
{
if (l2 != null ) prev.next = l2;
l1 = prev.next;
while (l1 != null )
{
sum = c + l1.data;
l1.data = sum % 10 ;
c = sum / 10 ;
prev = l1;
l1 = l1.next;
}
}
if (c > 0 ) prev.next = new Node(c);
return reverse(head);
}
public static void main(String[] args)
{
LinkedList l1 = new LinkedList();
l1.insert( 3 );
l1.insert( 6 );
l1.insert( 5 );
LinkedList l2 = new LinkedList();
l2.insert( 2 );
l2.insert( 4 );
l2.insert( 8 );
LinkedList l3 = new LinkedList();
Node head = sum(l1.head, l2.head);
l3.traverse(head);
System.out.print( "Null" );
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def traverse( self ):
linkedListStr = ""
temp = self .head
while temp:
linkedListStr + = str (temp.data) + " -> "
temp = temp. next
return linkedListStr + "NULL"
def insert( self , data):
newNode = Node(data)
if self .head is None :
self .head = newNode
else :
newNode. next = self .head
self .head = newNode
def reverse(Head):
if (Head is None and
Head. next is None ):
return Head
prev = None
curr = Head
while curr:
temp = curr. next
curr. next = prev
prev = curr
curr = temp
Head = prev
return Head
def listSum(l1, l2):
if l1 is None :
return l1
if l2 is None :
return l2
l1 = reverse(l1)
l2 = reverse(l2)
head = l1
prev = None
c = 0
sum = 0
while l1 is not None and l2 is not None :
sum = c + l1.data + l2.data
l1.data = sum % 10
c = int ( sum / 10 )
prev = l1
l1 = l1. next
l2 = l2. next
if l1 is not None or l2 is not None :
if l2 is not None :
prev. next = l2
l1 = prev. next
while l1 is not None :
sum = c + l1.data
l1.data = sum % 10
c = int ( sum / 10 )
prev = l1
l1 = l1. next
if c > 0 :
prev. next = Node(c)
return reverse(head)
linkedList1 = LinkedList()
linkedList1.insert( 3 )
linkedList1.insert( 6 )
linkedList1.insert( 5 )
linkedList2 = LinkedList()
linkedList2.insert( 2 )
linkedList2.insert( 4 )
linkedList2.insert( 8 )
linkedList3 = LinkedList()
linkedList3.head = listSum(linkedList1.head,
linkedList2.head)
print (linkedList3.traverse())
|
C#
using System;
public class Node
{
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
public class LinkedList
{
Node head;
void traverse(Node head)
{
while (head != null )
{
Console.Write(head.data + "->" );
head = head.next;
}
}
void insert( int x)
{
Node temp = new Node(x);
if (head == null ) head = temp;
else
{
temp.next = head;
head = temp;
}
}
public static Node reverse(Node head)
{
if (head == null ||
head.next == null ) return head;
Node prev = null ;
Node curr = head;
while (curr != null )
{
Node temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
head = prev;
return head;
}
public static Node sum(Node l1, Node l2)
{
if (l2 == null ) return l1;
if (l1 == null ) return l2;
l1 = reverse(l1);
l2 = reverse(l2);
Node head = l1;
Node prev = null ;
int c = 0,sum;
while (l1 != null && l2 != null )
{
sum = c + l1.data + l2.data;
l1.data = sum % 10;
c = sum / 10;
prev = l1;
l1 = l1.next;
l2 = l2.next;
}
if (l1 != null ||l2 != null )
{
if (l2 != null ) prev.next = l2;
l1 = prev.next;
while (l1 != null )
{
sum = c + l1.data;
l1.data = sum % 10;
c = sum / 10;
prev = l1;
l1 = l1.next;
}
}
if (c > 0) prev.next = new Node(c);
return reverse(head);
}
public static void Main(String[] args)
{
LinkedList l1 = new LinkedList();
l1.insert(3);
l1.insert(6);
l1.insert(5);
LinkedList l2 = new LinkedList();
l2.insert(2);
l2.insert(4);
l2.insert(8);
LinkedList l3 = new LinkedList();
Node head = sum(l1.head, l2.head);
l3.traverse(head);
Console.Write( "Null" );
}
}
|
Javascript
<script>
class Node
{
constructor(val)
{
this .data = val;
this .next = null ;
}
}
class LinkedList {
constructor()
{
this .head = null ;
}
insert(x)
{
var node = new Node();
node.data = x;
if ( this .head == null )
this .head = node;
else {
node.next = this .head;
this .head = node;
}
}
reverse()
{
var prev = null , curr = this .head;
while (curr) {
var temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
this .head = prev;
}
traverse()
{
var temp = this .head;
while (temp) {
document.write( temp.data + " -> " );
temp = temp.next;
}
document.write( "null" );
}
sum(l2)
{
this .reverse();
l2.reverse();
var start1 = this .head, start2 = l2.head;
var prev = null ;
var carry = 0;
while (start1 && start2) {
var temp = start1.data + start2.data + carry;
start1.data = temp % 10;
carry = parseInt(temp / 10);
prev = start1;
start1 = start1.next;
start2 = start2.next;
}
if (start1 || start2) {
if (start2)
prev.next = start2;
start1 = prev.next;
while (start1) {
var temp = start1.data + carry;
start1.data = temp % 10;
carry = parseInt(temp / 10);
prev = start1;
start1 = start1.next;
}
}
if (carry > 0) {
prev.next = new Node(carry);
}
this .reverse();
}
};
var l1 = new LinkedList();
l1.insert(3);
l1.insert(6);
l1.insert(5);
var l2 = new LinkedList();
l2.insert(2);
l2.insert(4);
l2.insert(8);
l1.sum(l2);
l1.traverse();
</script>
|
Output:
1 -> 4 -> 0 -> 5 -> NULL
Time Complexity: O(max(m, n)) where m and n are number of nodes in list l1 and list l2 respectively.
Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...