Add the given digit to a number stored in a linked list
Last Updated :
06 Jul, 2021
Given a linked list which represents an integer number where every node is a digit if the represented integer. The task is to add a given digit N to the represented integer.
Examples:
Input: 9 -> 9 -> 3 -> NULL, N = 7
Output:
9 -> 9 -> 3 -> NULL
1 -> 0 -> 0 -> 0 -> NULL
Input: 2 -> 9 -> 9 -> NULL, N = 5
Output:
2 -> 9 -> 9 -> NULL
3 -> 0 -> 4 -> NULL
Approach: We have already discussed the approach for adding 1 to a number stored in linked list int this article but the code requires reversal of the linked list.
In this post, we have extended the problem to adding any digit to the number stored in a linked list and achieving the same without reversal or recursion.
The idea is to traverse the list and while traversing maintain a pointer to the last node whose value is less than 9. This is because we are adding a single digit to the number stored in the linked list. So, the maximum value of carry (if present) can be 1. Suppose we start propagating the carry from the least significant digit towards most significant digit, then the propagation will stop as soon as it finds a number less than 9.
After the complete traversal of the list in this manner, we have finally reached the last node of the linked list and also maintained a pointer to the latest node whose value is less than 9.
Two cases can arise:
- There can be overflow after adding the number in the last digit i.e. value at the node is greater than 9.
- No overflow i.e. after adding the value at the node is less than 10.
In the first case, we have to propagate the carry from the latest node whose value is less than 9 to the last node.
In the second case, we don’t have to do anything else.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct node {
int key;
node* next;
node( int n)
{
key = n;
next = NULL;
}
};
class LinkedList {
node* head;
public :
LinkedList();
void insert(node* n);
void addDigit( int n);
void printList();
};
LinkedList::LinkedList()
{
head = NULL;
}
void LinkedList::insert(node* n)
{
if (head == NULL)
head = n;
else {
n->next = head;
head = n;
}
}
void LinkedList::printList()
{
node* ptr = head;
while (ptr) {
cout << ptr->key << " -> " ;
ptr = ptr->next;
}
cout << "NULL" << endl;
}
void LinkedList::addDigit( int n)
{
node* lastNode = NULL;
node* curr = head;
while (curr->next) {
if (curr->key < 9)
lastNode = curr;
curr = curr->next;
}
curr->key = curr->key + n;
if (curr->key > 9) {
curr->key = curr->key % 10;
if (lastNode == NULL) {
insert( new node(1));
lastNode = head->next;
}
while (lastNode != curr) {
lastNode->key = (lastNode->key + 1) % 10;
lastNode = lastNode->next;
}
}
}
int main()
{
LinkedList* l1 = new LinkedList();
l1->insert( new node(9));
l1->insert( new node(9));
l1->insert( new node(1));
l1->printList();
l1->addDigit(5);
l1->printList();
return 0;
}
|
Java
class node
{
int key;
node next;
node( int n)
{
key = n;
next = null ;
}
};
class LinkedList
{
static node head;
public LinkedList()
{
head = null ;
}
void insert(node n)
{
if (head == null )
head = n;
else
{
n.next = head;
head = n;
}
}
void printList()
{
node ptr = head;
while (ptr != null )
{
System.out.print(ptr.key + "->" );
ptr = ptr.next;
}
System.out.print( "null" + "\n" );
}
void addDigit( int n)
{
node lastNode = null ;
node curr = head;
while (curr.next != null )
{
if (curr.key < 9 )
lastNode = curr;
curr = curr.next;
}
curr.key = curr.key + n;
if (curr.key > 9 )
{
curr.key = curr.key % 10 ;
if (lastNode == null )
{
insert( new node( 1 ));
lastNode = head.next;
}
while (lastNode != curr)
{
lastNode.key = (lastNode.key + 1 ) % 10 ;
lastNode = lastNode.next;
}
}
}
public static void main(String[] args)
{
LinkedList l1 = new LinkedList();
l1.insert( new node( 9 ));
l1.insert( new node( 9 ));
l1.insert( new node( 1 ));
l1.printList();
l1.addDigit( 5 );
l1.printList();
}
}
|
Python3
class node:
def __init__( self , key):
self .key = key
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def insert( self , n):
if ( self .head = = None ):
self .head = n
else :
n. next = self .head;
self .head = n
def printList( self ):
ptr = self .head
while (ptr ! = None ):
print (ptr.key, end = ' -> ' )
ptr = ptr. next
print ( 'NULL' )
def addDigit( self , n):
lastNode = None
curr = self .head
while (curr. next ! = None ):
if (curr.key < 9 ):
lastNode = curr
curr = curr. next
curr.key = curr.key + n
if (curr.key > 9 ):
curr.key = curr.key % 10
if (lastNode = = None ):
self .insert(node( 1 ))
lastNode = self .head. next
while (lastNode ! = curr):
lastNode.key = (lastNode.key + 1 ) % 10
lastNode = lastNode. next
if __name__ = = '__main__' :
l1 = LinkedList()
l1.insert(node( 9 ))
l1.insert(node( 9 ))
l1.insert(node( 1 ))
l1.printList()
l1.addDigit( 5 )
l1.printList()
|
C#
using System;
public class node
{
public int key;
public node next;
public node( int n)
{
key = n;
next = null ;
}
};
public class List
{
static node head;
public List()
{
head = null ;
}
void insert(node n)
{
if (head == null )
head = n;
else
{
n.next = head;
head = n;
}
}
void printList()
{
node ptr = head;
while (ptr != null )
{
Console.Write(ptr.key + "->" );
ptr = ptr.next;
}
Console.Write( "null" + "\n" );
}
void addDigit( int n)
{
node lastNode = null ;
node curr = head;
while (curr.next != null )
{
if (curr.key < 9)
lastNode = curr;
curr = curr.next;
}
curr.key = curr.key + n;
if (curr.key > 9)
{
curr.key = curr.key % 10;
if (lastNode == null )
{
insert( new node(1));
lastNode = head.next;
}
while (lastNode != curr)
{
lastNode.key = (lastNode.key + 1) % 10;
lastNode = lastNode.next;
}
}
}
public static void Main(String[] args)
{
List l1 = new List();
l1.insert( new node(9));
l1.insert( new node(9));
l1.insert( new node(1));
l1.printList();
l1.addDigit(5);
l1.printList();
}
}
|
Javascript
<script>
class node
{
constructor(n)
{
this .key = n;
this .next = null
}
};
var head = null ;
function List()
{
head = null ;
}
function insert(n)
{
if (head == null )
head = n;
else
{
n.next = head;
head = n;
}
}
function printList()
{
var ptr = head;
while (ptr != null )
{
document.write(ptr.key + " -> " );
ptr = ptr.next;
}
document.write( "null" + "<br>" );
}
function addDigit(n)
{
var lastNode = null ;
var curr = head;
while (curr.next != null )
{
if (curr.key < 9)
lastNode = curr;
curr = curr.next;
}
curr.key = curr.key + n;
if (curr.key > 9)
{
curr.key = curr.key % 10;
if (lastNode == null )
{
insert( new node(1));
lastNode = head.next;
}
while (lastNode != curr)
{
lastNode.key = (lastNode.key + 1) % 10;
lastNode = lastNode.next;
}
}
}
insert( new node(9));
insert( new node(9));
insert( new node(1));
printList();
addDigit(5);
printList();
</script>
|
Output:
1 -> 9 -> 9 -> NULL
2 -> 0 -> 4 -> NULL
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...