{"id":27625,"date":"2018-04-04T19:36:41","date_gmt":"2018-04-04T14:06:41","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27625"},"modified":"2018-09-16T14:37:36","modified_gmt":"2018-09-16T09:07:36","slug":"add-1-number-represented-linked-list","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/add-1-number-represented-linked-list\/","title":{"rendered":"Cpp Algorithm-Add 1 to a number represented as linked list"},"content":{"rendered":"<p>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-&gt; 9-&gt; 9 -&gt; 9) and adding 1 to it should change it to (2-&gt;0-&gt;0-&gt;0)<\/p>\n<p>Below are the steps :<\/p>\n<ol>\n<li>Reverse given linked list. For example, 1-&gt; 9-&gt; 9 -&gt; 9 is converted to 9-&gt; 9 -&gt; 9 -&gt;1.<\/li>\n<li>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.<\/li>\n<li>Reverse modified linked list and return head.<\/li>\n<\/ol>\n<p><strong>C++ prpgramming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ C++ program to add 1 to a linked list<br\/>#include&lt;bits\/stdc++.h&gt;<br\/> <br\/>\/* Linked list node *\/<br\/>struct Node<br\/>{<br\/>    int data;<br\/>    Node* next;<br\/>};<br\/> <br\/>\/* Function to create a new node with given data *\/<br\/>Node *newNode(int data)<br\/>{<br\/>    Node *new_node = new Node;<br\/>    new_node-&gt;data = data;<br\/>    new_node-&gt;next = NULL;<br\/>    return new_node;<br\/>}<br\/> <br\/>\/* Function to reverse the linked list *\/<br\/>Node *reverse(Node *head)<br\/>{<br\/>    Node * prev   = NULL;<br\/>    Node * current = head;<br\/>    Node * next;<br\/>    while (current != NULL)<br\/>    {<br\/>        next  = current-&gt;next;<br\/>        current-&gt;next = prev;<br\/>        prev = current;<br\/>        current = next;<br\/>    }<br\/>    return prev;<br\/>}<br\/> <br\/>\/* Adds one to a linked lists and return the head<br\/>   node of resultant list *\/<br\/>Node *addOneUtil(Node *head)<br\/>{<br\/>    \/\/ res is head node of the resultant list<br\/>    Node* res = head;<br\/>    Node *temp, *prev = NULL;<br\/> <br\/>    int carry = 1, sum;<br\/> <br\/>    while (head != NULL) \/\/while both lists exist<br\/>    {<br\/>        \/\/ Calculate value of next digit in resultant list.<br\/>        \/\/ The next digit is sum of following things<br\/>        \/\/ (i) Carry<br\/>        \/\/ (ii) Next digit of head list (if there is a<br\/>        \/\/     next digit)<br\/>        sum = carry + head-&gt;data;<br\/> <br\/>        \/\/ update carry for next calulation<br\/>        carry = (sum &gt;= 10)? 1 : 0;<br\/> <br\/>        \/\/ update sum if it is greater than 10<br\/>        sum = sum % 10;<br\/> <br\/>        \/\/ Create a new node with sum as data<br\/>        head-&gt;data = sum;<br\/> <br\/>        \/\/ Move head and second pointers to next nodes<br\/>        temp = head;<br\/>        head = head-&gt;next;<br\/>    }<br\/> <br\/>    \/\/ if some carry is still there, add a new node to<br\/>    \/\/ result list.<br\/>    if (carry &gt; 0)<br\/>        temp-&gt;next = newNode(carry);<br\/> <br\/>    \/\/ return head of the resultant list<br\/>    return res;<br\/>}<br\/> <br\/>\/\/ This function mainly uses addOneUtil().<br\/>Node* addOne(Node *head)<br\/>{<br\/>    \/\/ Reverse linked list<br\/>    head = reverse(head);<br\/> <br\/>    \/\/ Add one from left to right of reversed<br\/>    \/\/ list<br\/>    head = addOneUtil(head);<br\/> <br\/>    \/\/ Reverse the modified list<br\/>    return reverse(head);<br\/>}<br\/> <br\/>\/\/ A utility function to print a linked list<br\/>void printList(Node *node)<br\/>{<br\/>    while (node != NULL)<br\/>    {<br\/>        printf(&quot;%d&quot;, node-&gt;data);<br\/>        node = node-&gt;next;<br\/>    }<br\/>    printf(&quot;\\n&quot;);<br\/>}<br\/> <br\/>\/* Driver program to test above function *\/<br\/>int main(void)<br\/>{<br\/>    Node *head = newNode(1);<br\/>    head-&gt;next = newNode(9);<br\/>    head-&gt;next-&gt;next = newNode(9);<br\/>    head-&gt;next-&gt;next-&gt;next = newNode(9);<br\/> <br\/>    printf(&quot;List is &quot;);<br\/>    printList(head);<br\/> <br\/>    head = addOne(head);<br\/> <br\/>    printf(&quot;\\nResultant list is &quot;);<br\/>    printList(head);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>List is 1999\r\n\r\nResultant list is 2000\r\n\r\n<\/pre>\n<p><strong>\u00a0Recursive Implementation:<\/strong><br \/>\nWe can recursively reach the last node and forward carry to previous nodes. Recursive solution doesn\u2019t require reversing of linked list. We can also use a stack in place of recursion to temporarily hold nodes.<\/p>\n<p><strong>C++ Programming:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-cpp code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-cpp code-embed-code\">\/\/ Recursive C++ program to add 1 to a linked list<br\/>#include&lt;bits\/stdc++.h&gt;<br\/> <br\/>\/* Linked list node *\/<br\/>struct Node<br\/>{<br\/>    int data;<br\/>    Node* next;<br\/>};<br\/> <br\/>\/* Function to create a new node with given data *\/<br\/>Node *newNode(int data)<br\/>{<br\/>    Node *new_node = new Node;<br\/>    new_node-&gt;data = data;<br\/>    new_node-&gt;next = NULL;<br\/>    return new_node;<br\/>}<br\/> <br\/>\/\/ Recursively add 1 from end to beginning and returns<br\/>\/\/ carry after all nodes are processed.<br\/>int addWithCarry(Node *head)<br\/>{<br\/>    \/\/ If linked list is empty, then<br\/>    \/\/ return carry<br\/>    if (head == NULL)<br\/>        return 1;<br\/> <br\/>    \/\/ Add carry returned be next node call<br\/>    int res = head-&gt;data + addWithCarry(head-&gt;next);<br\/> <br\/>    \/\/ Update data and return new carry<br\/>    head-&gt;data = (res) % 10;<br\/>    return (res) \/ 10;<br\/>}<br\/> <br\/>\/\/ This function mainly uses addWithCarry().<br\/>Node* addOne(Node *head)<br\/>{<br\/>    \/\/ Add 1 to linked list from end to beginning<br\/>    int carry = addWithCarry(head);<br\/> <br\/>    \/\/ If there is carry after processing all nodes,<br\/>    \/\/ then we need to add a new node to linked list<br\/>    if (carry)<br\/>    {<br\/>        Node *newNode = new Node;<br\/>        newNode-&gt;data = carry;<br\/>        newNode-&gt;next = head;<br\/>        return newNode; \/\/ New node becomes head now<br\/>    }<br\/> <br\/>    return head;<br\/>}<br\/> <br\/>\/\/ A utility function to print a linked list<br\/>void printList(Node *node)<br\/>{<br\/>    while (node != NULL)<br\/>    {<br\/>        printf(&quot;%d&quot;, node-&gt;data);<br\/>        node = node-&gt;next;<br\/>    }<br\/>    printf(&quot;\\n&quot;);<br\/>}<br\/> <br\/>\/* Driver program to test above function *\/<br\/>int main(void)<br\/>{<br\/>    Node *head = newNode(1);<br\/>    head-&gt;next = newNode(9);<br\/>    head-&gt;next-&gt;next = newNode(9);<br\/>    head-&gt;next-&gt;next-&gt;next = newNode(9);<br\/> <br\/>    printf(&quot;List is &quot;);<br\/>    printList(head);<br\/> <br\/>    head = addOne(head);<br\/> <br\/>    printf(&quot;\\nResultant list is &quot;);<br\/>    printList(head);<br\/> <br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>List is 1999\r\n\r\nResultant list is 2000\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cpp Algorithm-Add 1 to a number represented as linked list-Linked list<br \/>\nNumber is represented in linked list such that each digit corresponds<\/p>\n","protected":false},"author":1,"featured_media":31292,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79476,79478],"tags":[81891,81893,81885,81887,81886,81889,81888,81890,81894,81892,81884],"class_list":["post-27625","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linked-list","category-singly-linked-list","tag-1s-and-2s","tag-add-1-to-the-integer-represented-by-a-linked-list-with-on-time-and-o1-space","tag-add-2-linked-lists","tag-add-one-to-number-array","tag-add-two-linked-lists-c","tag-find-next-greater-element-with-same-digits","tag-get-a-number","tag-given-a-linked-list-of-0s","tag-how-to-find-if-words-in-a-file-are-anagram","tag-sort-it","tag-sum-of-two-linked-lists-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27625","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27625"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27625\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media\/31292"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}