Javascript Program For Adding 1 To A Number Represented As Linked List
Last Updated :
26 Mar, 2023
Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0)
Below are the steps :
- Reverse given linked list. For example, 1-> 9-> 9 -> 9 is converted to 9-> 9 -> 9 ->1.
- Start traversing linked list from leftmost node and add 1 to it. If there is a carry, move to the next node. Keep moving to the next node while there is a carry.
- Reverse modified linked list and return head.
Below is the implementation of above steps.
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
};
function newNode(data)
{
var new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
function reverse(head)
{
var prev = null ;
var current = head;
var next;
while (current != null )
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
function addOneUtil(head)
{
var res = head;
var temp, prev = null ;
var carry = 1, sum;
while (head != null )
{
sum = carry + head.data;
carry = (sum >= 10)? 1 : 0;
sum = sum % 10;
head.data = sum;
temp = head;
head = head.next;
}
if (carry > 0)
temp.next = newNode(carry);
return res;
}
function addOne(head)
{
head = reverse(head);
head = addOneUtil(head);
return reverse(head);
}
function printList(node)
{
while (node != null )
{
document.write(node.data);
node = node.next;
}
document.write( "<br>" );
}
var head = newNode(1);
head.next = newNode(9);
head.next.next = newNode(9);
head.next.next.next = newNode(9);
document.write( "List is " );
printList(head);
head = addOne(head);
document.write( "<br>Resultant list is " );
printList(head);
</script>
|
Output:
List is 1999
Resultant list is 2000
Time Complexity: O(n)
Auxiliary Space: O(1)
Recursive Implementation:
We can recursively reach the last node and forward carry to previous nodes. Recursive solution doesn’t require reversing of linked list. We can also use a stack in place of recursion to temporarily hold nodes.
Below is the implementation of recursive solution.
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
}
function newNode(data)
{
var new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
function addWithCarry(head)
{
if (head == null ) return 1;
var res = head.data + addWithCarry(head.next);
head.data = res % 10;
return parseInt(res / 10);
}
function addOne(head)
{
var carry = addWithCarry(head);
var newNodes = null ;
if (carry > 0)
{
newNodes = newNode(carry);
newNodes.next = head;
return newNodes;
}
return head;
}
function printList(node)
{
while (node != null )
{
document.write(node.data);
node = node.next;
}
document.write( "<br>" );
}
var head = newNode(1);
head.next = newNode(9);
head.next.next = newNode(9);
head.next.next.next = newNode(9);
document.write( "List is " );
printList(head);
head = addOne(head);
document.write( "<br>" );
document.write( "Resultant list is " );
printList(head);
</script>
|
Output:
List is 1999
Resultant list is 2000
Please refer complete article on Add 1 to a number represented as linked list for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...