Java Program For Adding 1 To A Number Represented As Linked List
Last Updated :
29 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.
Java
class GfG
{
static class Node
{
int data;
Node next;
}
static Node newNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
static Node reverse(Node head)
{
Node prev = null ;
Node current = head;
Node next = null ;
while (current != null )
{
next = current.next;
current.next = prev;
prev = current;
current = next;
}
return prev;
}
static Node addOneUtil(Node head)
{
Node res = head;
Node temp = null , prev = null ;
int 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;
}
static Node addOne(Node head)
{
head = reverse(head);
head = addOneUtil(head);
return reverse(head);
}
static void printList(Node node)
{
while (node != null )
{
System.out.print(node.data);
node = node.next;
}
System.out.println();
}
public static void main(String[] args)
{
Node head = newNode( 1 );
head.next = newNode( 9 );
head.next.next = newNode( 9 );
head.next.next.next = newNode( 9 );
System.out.print( "List is " );
printList(head);
head = addOne(head);
System.out.println();
System.out.print(
"Resultant list is " );
printList(head);
}
}
|
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.
Java
class GfG
{
static class Node
{
int data;
Node next;
}
static Node newNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
static int addWithCarry(Node head)
{
if (head == null )
return 1 ;
int res = head.data + addWithCarry(head.next);
head.data = (res) % 10 ;
return (res) / 10 ;
}
static Node addOne(Node head)
{
int carry = addWithCarry(head);
if (carry > 0 )
{
Node newNode = newNode(carry);
newNode.next = head;
return newNode;
}
return head;
}
static void printList(Node node)
{
while (node != null )
{
System.out.print(node.data);
node = node.next;
}
System.out.println();
}
public static void main(String[] args)
{
Node head = newNode( 1 );
head.next = newNode( 9 );
head.next.next = newNode( 9 );
head.next.next.next = newNode( 9 );
System.out.print( "List is " );
printList(head);
head = addOne(head);
System.out.println();
System.out.print( "Resultant list is " );
printList(head);
}
}
|
Output:
List is 1999
Resultant list is 2000
Simple approach and easy implementation: The idea is to store the last non 9 digit pointer so that if the last pointer is zero we can replace all the nodes after stored node(which contains the location of last digit before 9) to 0 and add the value of the stored node by 1
Java
class GFG{
static class Node
{
int data;
Node next;
}
static Node newNode( int data)
{
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
static Node addOne(Node head)
{
Node ln = head;
if (head.next == null )
{
head.data += 1 ;
return head;
}
Node t = head;
int prev;
while (t.next != null )
{
if (t.data != 9 )
{
ln = t;
}
t = t.next;
}
if (t.data == 9 && ln != null )
{
t = ln;
t.data += 1 ;
t = t.next;
while (t != null )
{
t.data = 0 ;
t = t.next;
}
}
else
{
t.data += 1 ;
}
return head;
}
static void printList(Node node)
{
while (node != null )
{
System.out.print(node.data);
node = node.next;
}
System.out.println();
}
public static void main(String[] args)
{
Node head = newNode( 1 );
head.next = newNode( 9 );
head.next.next = newNode( 9 );
head.next.next.next = newNode( 9 );
System.out.print( "List is " );
printList(head);
head = addOne(head);
System.out.println();
System.out.print( "Resultant list is " );
printList(head);
}
}
|
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...