# 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 :

1. Reverse given linked list. For example, 1-> 9-> 9 -> 9 is converted to 9-> 9 -> 9 ->1.
2. 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.

Below is the implementation of above steps.

## Javascript

 ``

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

 ``

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!

Previous
Next