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.
Input: 9 -> 9 -> 3 -> NULL, N = 7
9 -> 9 -> 3 -> NULL
1 -> 0 -> 0 -> 0 -> NULL
Input: 2 -> 9 -> 9 -> NULL, N = 5
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:
1 -> 9 -> 9 -> NULL 2 -> 0 -> 4 -> NULL
- Create new linked list from two given linked list with greater element at each node
- Add 1 to a number represented as linked list
- Add one to a number represented as linked list | Set 2
- Convert singly linked list into circular linked list
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Difference between Singly linked list and Doubly linked list
- Merge a linked list into another linked list at alternate positions
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Sum of all nodes in a doubly linked list divisible by a given number K
- Large number arithmetic using doubly linked list
- Number of connected components in a doubly linked list
- Product of all nodes in a doubly linked list divisible by a given number K
- Maximum and Minimum element of a linked list which is divisible by a given number k
- Write a function that counts the number of times a given int occurs in a Linked List
- 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.