Given a singly linked list which represents a number where each node contains only one digit [0 – 9]. The task is to add 1 to the number represented by the given linked list and print the new linked list.
Input: 1 -> 2 -> 9 -> 9 -> NULL
Original list is : 1 2 9 9
Resultant list is : 1 3 0 0
Input: 9 -> 9 -> 9 -> 9 -> NULL
Original list is : 9 9 9 9
Resultant list is : 1 0 0 0 0
Approach: A previous implementation of the above problem problem was discussed in this post. However, one of the implementations requires the linked list to be reversed and the other makes use of recursion. An O(1) space complexity solution is been discussed here which doesn’t require the linked list to be reversed.
The main focus in this question is on the digit 9 which creates all the changes otherwise for other digits we have to just increment their value by 1 but if we change node’s value with value 9 it makes a carry which then has to be passed through the linked list.
Find the last node in the linked list which is not equal to 9. Now there are three cases:
- If there is no such node i.e. the value of every node is 9 then the new linked list will contain all 0s and a single 1 inserted at the head of the linked list.
- If the rightmost node which is not equal to 9 is the last node in the linked list then add 1 to this node and return the head of the linked list.
- If the node is other than the last node i.e. every node after it is equal to 9 then add 1 to the current node and change all the nodes after it to 0.
Below is the implementation of the above approach:
Original list is : 1 2 9 9 Resultant list is : 1 3 0 0
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Add 1 to a number represented as linked list
- Add Two Numbers Represented by Linked Lists | Set 3
- Add two numbers represented by linked lists | Set 1
- Add two numbers represented by linked lists | Set 2
- Multiply two numbers represented as linked lists into a third list
- Reverse all the word in a String represented as a Linked List
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Add the given digit to a number stored in a linked list using recursion
- Add the given digit to a number stored in a linked list
- Compare two strings represented as linked lists
- Subtract Two Numbers represented as Linked Lists
- Compare numbers represented by Linked Lists
- Multiply two numbers represented by Linked Lists
- Merge a linked list into another linked list at alternate positions
- Convert singly linked list into circular linked list
- Difference between Singly linked list and Doubly linked list
- Convert Singly Linked List to XOR Linked List
- Create new linked list from two given linked list with greater element at each node
- Check if a linked list is Circular Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.