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 -> NULLInput: 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++ implementation of the approach
#include <iostream>
using namespace std;
// Node structure containing data
// and pointer to the next Node
struct node {
int key;
node* next;
node(int n)
{
key = n;
next = NULL;
}
};
// Linked list class
class LinkedList {
node* head;
public:
// Default constructor for
// creating empty list
LinkedList();
// Insert a node in linked list
void insert(node* n);
// Adding a single digit to the list
void addDigit(int n);
// Print the linked list
void printList();
};
LinkedList::LinkedList()
{
// Empty List
head = NULL;
}
// Function to insert a node at the
// head of the linked list
void LinkedList::insert(node* n)
{
// Empty List
if (head == NULL)
head = n;
// Insert in the beginning of the list
else {
n->next = head;
head = n;
}
}
// Function to print the linked list
void LinkedList::printList()
{
node* ptr = head;
while (ptr) {
cout << ptr->key << " -> ";
ptr = ptr->next;
}
cout << "NULL" << endl;
}
// Function to add a digit to the integer
// represented as a linked list
void LinkedList::addDigit(int n)
{
// To keep track of the last node
// whose value is less than 9
node* lastNode = NULL;
node* curr = head;
while (curr->next) {
// If found a node with value
// less than 9
if (curr->key < 9)
lastNode = curr;
// Otherwise keep traversing
// the list till end
curr = curr->next;
}
// Add the given digit to the last node
curr->key = curr->key + n;
// In case of overflow in the last node
if (curr->key > 9) {
curr->key = curr->key % 10;
// If the list is of the
// form 9 -> 9 -> 9 -> ...
if (lastNode == NULL) {
// Insert a node at the beginning as
// there would be overflow in the
// head in this case
insert(new node(1));
// Adjust the lastNode pointer to
// propagate the carry effect to
// all the nodes of the list
lastNode = head->next;
}
// Forward propagate carry effect
while (lastNode != curr) {
lastNode->key = (lastNode->key + 1) % 10;
lastNode = lastNode->next;
}
}
}
// Driver code
int main()
{
// Creating the linked list
LinkedList* l1 = new LinkedList();
// Adding elements to the linked list
l1->insert(new node(9));
l1->insert(new node(9));
l1->insert(new node(1));
// Printing the original list
l1->printList();
// Adding the digit
l1->addDigit(5);
// Printing the modified list
l1->printList();
return 0;
}
// Java implementation of the approach
// Node structure containing data
// and pointer to the next Node
class node
{
int key;
node next;
node(int n)
{
key = n;
next = null;
}
};
// Linked list class
class LinkedList
{
static node head;
// Default constructor for
// creating empty list
public LinkedList()
{
// Empty List
head = null;
}
// Function to insert a node at the
// head of the linked list
void insert(node n)
{
// Empty List
if (head == null)
head = n;
// Insert in the beginning of the list
else
{
n.next = head;
head = n;
}
}
// Function to print the linked list
void printList()
{
node ptr = head;
while (ptr != null)
{
System.out.print(ptr.key + "->");
ptr = ptr.next;
}
System.out.print("null" + "\n");
}
// Function to add a digit to the integer
// represented as a linked list
void addDigit(int n)
{
// To keep track of the last node
// whose value is less than 9
node lastNode = null;
node curr = head;
while (curr.next != null)
{
// If found a node with value
// less than 9
if (curr.key < 9)
lastNode = curr;
// Otherwise keep traversing
// the list till end
curr = curr.next;
}
// Add the given digit to the last node
curr.key = curr.key + n;
// In case of overflow in the last node
if (curr.key > 9)
{
curr.key = curr.key % 10;
// If the list is of the
// form 9.9.9....
if (lastNode == null)
{
// Insert a node at the beginning as
// there would be overflow in the
// head in this case
insert(new node(1));
// Adjust the lastNode pointer to
// propagate the carry effect to
// all the nodes of the list
lastNode = head.next;
}
// Forward propagate carry effect
while (lastNode != curr)
{
lastNode.key = (lastNode.key + 1) % 10;
lastNode = lastNode.next;
}
}
}
// Driver code
public static void main(String[] args)
{
// Creating the linked list
LinkedList l1 = new LinkedList();
// Adding elements to the linked list
l1.insert(new node(9));
l1.insert(new node(9));
l1.insert(new node(1));
// Printing the original list
l1.printList();
// Adding the digit
l1.addDigit(5);
// Printing the modified list
l1.printList();
}
}
// This code is contributed by Rajput-Ji
# Python3 implementation of the approach
# Node structure containing data
# and pointer to the next Node
class node:
def __init__(self, key):
self.key = key
self.next = None
# Linked list class
class LinkedList:
def __init__(self):
self.head = None
# Function to insert a node at the
# head of the linked list
def insert(self, n):
# Empty List
if (self.head == None):
self.head = n
# Insert in the beginning of the list
else:
n.next = self.head;
self.head = n
# Function to print the linked list
def printList(self):
ptr = self.head
while (ptr != None):
print(ptr.key, end = ' -> ')
ptr = ptr.next
print('NULL')
# Function to add a digit to the integer
# represented as a linked list
def addDigit(self, n):
# To keep track of the last node
# whose value is less than 9
lastNode = None
curr = self.head
while (curr.next != None):
# If found a node with value
# less than 9
if (curr.key < 9):
lastNode = curr
# Otherwise keep traversing
# the list till end
curr = curr.next
# Add the given digit to the last node
curr.key = curr.key + n
# In case of overflow in the last node
if (curr.key > 9):
curr.key = curr.key % 10
# If the list is of the
# form 9 . 9 . 9 . ...
if (lastNode == None):
# Insert a node at the beginning as
# there would be overflow in the
# self.head in this case
self.insert(node(1))
# Adjust the lastNode pointer to
# propagate the carry effect to
# all the nodes of the list
lastNode = self.head.next
# Forward propagate carry effect
while (lastNode != curr):
lastNode.key = (lastNode.key + 1) % 10
lastNode = lastNode.next
# Driver code
if __name__=='__main__':
# Creating the linked list
l1 = LinkedList()
# Adding elements to the linked list
l1.insert(node(9))
l1.insert(node(9))
l1.insert(node(1))
# Printing the original list
l1.printList()
# Adding the digit
l1.addDigit(5)
# Printing the modified list
l1.printList()
# This code is contributed by rutvik_56
// C# implementation of the approach
using System;
// Node structure containing data
// and pointer to the next Node
public class node
{
public int key;
public node next;
public node(int n)
{
key = n;
next = null;
}
};
// Linked list class
public class List
{
static node head;
// Default constructor for
// creating empty list
public List()
{
// Empty List
head = null;
}
// Function to insert a node at the
// head of the linked list
void insert(node n)
{
// Empty List
if (head == null)
head = n;
// Insert in the beginning of the list
else
{
n.next = head;
head = n;
}
}
// Function to print the linked list
void printList()
{
node ptr = head;
while (ptr != null)
{
Console.Write(ptr.key + "->");
ptr = ptr.next;
}
Console.Write("null" + "\n");
}
// Function to add a digit to the integer
// represented as a linked list
void addDigit(int n)
{
// To keep track of the last node
// whose value is less than 9
node lastNode = null;
node curr = head;
while (curr.next != null)
{
// If found a node with value
// less than 9
if (curr.key < 9)
lastNode = curr;
// Otherwise keep traversing
// the list till end
curr = curr.next;
}
// Add the given digit to the last node
curr.key = curr.key + n;
// In case of overflow in the last node
if (curr.key > 9)
{
curr.key = curr.key % 10;
// If the list is of the
// form 9.9.9....
if (lastNode == null)
{
// Insert a node at the beginning as
// there would be overflow in the
// head in this case
insert(new node(1));
// Adjust the lastNode pointer to
// propagate the carry effect to
// all the nodes of the list
lastNode = head.next;
}
// Forward propagate carry effect
while (lastNode != curr)
{
lastNode.key = (lastNode.key + 1) % 10;
lastNode = lastNode.next;
}
}
}
// Driver code
public static void Main(String[] args)
{
// Creating the linked list
List l1 = new List();
// Adding elements to the linked list
l1.insert(new node(9));
l1.insert(new node(9));
l1.insert(new node(1));
// Printing the original list
l1.printList();
// Adding the digit
l1.addDigit(5);
// Printing the modified list
l1.printList();
}
}
// This code is contributed by 29AjayKumar
<script>
// JavaScript implementation of the approach
// Node structure containing data
// and pointer to the next Node
class node
{
constructor(n)
{
this.key = n;
this.next = null
}
};
// Linked list class
var head = null;
// Default constructor for
// creating empty list
function List()
{
// Empty List
head = null;
}
// Function to insert a node at the
// head of the linked list
function insert(n)
{
// Empty List
if (head == null)
head = n;
// Insert in the beginning of the list
else
{
n.next = head;
head = n;
}
}
// Function to print the linked list
function printList()
{
var ptr = head;
while (ptr != null)
{
document.write(ptr.key + " -> ");
ptr = ptr.next;
}
document.write("null" + "<br>");
}
// Function to add a digit to the integer
// represented as a linked list
function addDigit(n)
{
// To keep track of the last node
// whose value is less than 9
var lastNode = null;
var curr = head;
while (curr.next != null)
{
// If found a node with value
// less than 9
if (curr.key < 9)
lastNode = curr;
// Otherwise keep traversing
// the list till end
curr = curr.next;
}
// Add the given digit to the last node
curr.key = curr.key + n;
// In case of overflow in the last node
if (curr.key > 9)
{
curr.key = curr.key % 10;
// If the list is of the
// form 9.9.9....
if (lastNode == null)
{
// Insert a node at the beginning as
// there would be overflow in the
// head in this case
insert(new node(1));
// Adjust the lastNode pointer to
// propagate the carry effect to
// all the nodes of the list
lastNode = head.next;
}
// Forward propagate carry effect
while (lastNode != curr)
{
lastNode.key = (lastNode.key + 1) % 10;
lastNode = lastNode.next;
}
}
}
// Driver code
// Adding elements to the linked list
insert(new node(9));
insert(new node(9));
insert(new node(1));
// Printing the original list
printList();
// Adding the digit
addDigit(5);
// Printing the modified list
printList();
</script>
Output:
1 -> 9 -> 9 -> NULL 2 -> 0 -> 4 -> NULL