# Add the given digit to a number stored in a linked list

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.

Examples:

Input: 9 -> 9 -> 3 -> NULL, N = 7
Output:
9 -> 9 -> 3 -> NULL
1 -> 0 -> 0 -> 0 -> NULL

Input: 2 -> 9 -> 9 -> NULL, N = 5
Output:
2 -> 9 -> 9 -> NULL
3 -> 0 -> 4 -> NULL

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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:

1. There can be overflow after adding the number in the last digit i.e. value at the node is greater than 9.
2. 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:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Node structure containing data ` `// and pointer to the next Node ` `struct` `node { ` ` `  `    ``int` `key; ` `    ``node* next; ` ` `  `    ``node(``int` `n) ` `    ``{ ` `        ``key = n; ` `        ``next = NULL; ` `    ``} ` `}; ` ` `  `// Linked list class ` `class` `LinkedList { ` ` `  `    ``node* head; ` ` `  `public``: ` `    ``// Deafault constructor for ` `    ``// creating empty list ` `    ``LinkedList(); ` ` `  `    ``// Insert a node in linked list ` `    ``void` `insert(node* n); ` ` `  `    ``// Adding a single digit to the list ` `    ``void` `addDigit(``int` `n); ` ` `  `    ``// Print the linked list ` `    ``void` `printList(); ` `}; ` ` `  `LinkedList::LinkedList() ` `{ ` `    ``// Empty List ` `    ``head = NULL; ` `} ` ` `  `// Function to insert a node at the ` `// head of the linked list ` `void` `LinkedList::insert(node* n) ` `{ ` `    ``// Empty List ` `    ``if` `(head == NULL) ` `        ``head = n; ` ` `  `    ``// Insert in the beginning of the list ` `    ``else` `{ ` `        ``n->next = head; ` `        ``head = n; ` `    ``} ` `} ` ` `  `// Function to print the linked list ` `void` `LinkedList::printList() ` `{ ` `    ``node* ptr = head; ` ` `  `    ``while` `(ptr) { ` `        ``cout << ptr->key << ``" -> "``; ` `        ``ptr = ptr->next; ` `    ``} ` `    ``cout << ``"NULL"` `<< endl; ` `} ` ` `  `// Function to add a digit to the integer ` `// represented as a linked list ` `void` `LinkedList::addDigit(``int` `n) ` `{ ` ` `  `    ``// To keep track of the last node ` `    ``// whose value is less than 9 ` `    ``node* lastNode = NULL; ` `    ``node* curr = head; ` ` `  `    ``while` `(curr->next) { ` ` `  `        ``// If found a node with value ` `        ``// less than 9 ` `        ``if` `(curr->key < 9) ` `            ``lastNode = curr; ` ` `  `        ``// Otherwise keep traversing ` `        ``// the list till end ` `        ``curr = curr->next; ` `    ``} ` ` `  `    ``// Add the given digit to the last node ` `    ``curr->key = curr->key + n; ` ` `  `    ``// In case of overflow in the last node ` `    ``if` `(curr->key > 9) { ` ` `  `        ``curr->key = curr->key % 10; ` ` `  `        ``// If the list is of the ` `        ``// form 9 -> 9 -> 9 -> ... ` `        ``if` `(lastNode == NULL) { ` ` `  `            ``// Insert a node at the beginnig as ` `            ``// there would be overflow in the ` `            ``// head in this case ` `            ``insert(``new` `node(1)); ` ` `  `            ``// Adjust the lastNode pointer to ` `            ``// propagate the carry effect to ` `            ``// all the nodes of the list ` `            ``lastNode = head->next; ` `        ``} ` ` `  `        ``// Forward propagate carry effect ` `        ``while` `(lastNode != curr) { ` `            ``lastNode->key = (lastNode->key + 1) % 10; ` `            ``lastNode = lastNode->next; ` `        ``} ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``// Creating the linked list ` `    ``LinkedList* l1 = ``new` `LinkedList(); ` ` `  `    ``// Adding elements to the linked list ` `    ``l1->insert(``new` `node(9)); ` `    ``l1->insert(``new` `node(9)); ` `    ``l1->insert(``new` `node(1)); ` ` `  `    ``// Printing the original list ` `    ``l1->printList(); ` ` `  `    ``// Adding the digit ` `    ``l1->addDigit(5); ` ` `  `    ``// Printing the modified list ` `    ``l1->printList(); ` ` `  `    ``return` `0; ` `} `

Output:

```1 -> 9 -> 9 -> NULL
2 -> 0 -> 4 -> NULL
```

My Personal Notes arrow_drop_up Completely in love with Data Structure and Algorithms and GeeksForGeeks provides me with the opportunity to write my own articles, review and improve articles of others thereby strengthening my skills and it makes spreading and improving knowledge more fun than it earlier was

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.

Article Tags :
Practice Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7432732,"math_prob":0.85714674,"size":5515,"snap":"2019-51-2020-05","text_gpt3_token_len":1431,"char_repetition_ratio":0.15768462,"word_repetition_ratio":0.09564394,"special_character_ratio":0.28485948,"punctuation_ratio":0.089060985,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98906165,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-07T21:10:30Z\",\"WARC-Record-ID\":\"<urn:uuid:581ec6de-e7da-4e37-a985-67a361489cc9>\",\"Content-Length\":\"148145\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a836a95d-1f7d-4574-87c4-b11c67ef1cc9>\",\"WARC-Concurrent-To\":\"<urn:uuid:b247fe31-5377-4e78-bc96-1dc1d8dccd45>\",\"WARC-IP-Address\":\"104.96.221.51\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/add-the-given-digit-to-a-number-stored-in-a-linked-list/\",\"WARC-Payload-Digest\":\"sha1:Y2JDDN5QAW2YIJPI3SDCHKUFA6VCMJJH\",\"WARC-Block-Digest\":\"sha1:R53RHYELEQ3TVUSQB4DPGDIBDFARE4Z7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540502120.37_warc_CC-MAIN-20191207210620-20191207234620-00224.warc.gz\"}"}