{"id":4438,"date":"2021-08-26T11:18:58","date_gmt":"2021-08-26T11:18:58","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=4438"},"modified":"2022-11-08T07:59:27","modified_gmt":"2022-11-08T07:59:27","slug":"add-1-to-number-represented-as-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/","title":{"rendered":"Add 1 To Number Represented As Linked List"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185498768-Add%20to%201%20Number_Img1.png\" alt=\"\" \/><\/p>\n<h3>Introduction<\/h3>\n<p>In the article, we will learn to add 1 to a number represented as linked list.As we know a linked list is a linear data structure. In a linked list, the elements are linked using pointers and each node points to the next node. Let&#8217;s try to understand how we can 1 to a number represented as linked list. <\/p>\n<h3>Problem Statement<\/h3>\n<p>In this problem, we are given a number represented in the form of a linked list, we are required to add 1 to it and report the answer as a linked list.<\/p>\n<p><strong>For Example:<\/strong> Given is a Number 19999 in the form of a linked list, and after adding 1 to linked list, it should give the result as follows:\n<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/input-21.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/output-11.png\" alt=\"\" \/><\/p>\n<h3>Problem Statement Understanding to add 1 to linked list<\/h3>\n<p>The problem statement is quite straightforward, we are provided with a number represented in the form of a <a href=\"https:\/\/prepbytes.com\/blog\/linked-list\/squarerootn-th-node-in-a-linked-list\/\" title=\"linked list\">linked list<\/a> which means we will be getting the head\/root of the linked list as our argument, and we have to add 1 to the number represented as linked list.<\/p>\n<p>Let&#8217;s learn programming languages online and try to understand to add 1 to linked list more clearly using an example.<\/p>\n<p>Let&#8217;s assume we are given the number 19999. So the linked list will be :<\/p>\n<ul>\n<li>Initial Linked List: 1 \u2192 9 \u2192 9 \u2192 9 \u2192 9<\/li>\n<li>We will be getting the head pointing to the 1<sup>st<\/sup> node of the linked list, which is 1 as our argument.<\/li>\n<li>So the head pointer will point to the first node:<br \/>\nhead \u21921 \u2192 9 \u2192 9 \u2192 9 \u2192 9<\/li>\n<li>After we add 1 to linked list, our linked list will become:<br \/>\nhead \u21922 \u2192 0 \u2192 0 \u2192 0 \u2192 0<\/li>\n<li>Which is the result after adding 1 to 19999 = 20000<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Add-1-To-Number-Represented-As-Linked-List-6.png\" alt=\"\" \/><\/p>\n<p>At this point in time, we have understood how to add 1 to a number represented as linked list. Now try to formulate an approach to add 1 to a number represented as linked list.<\/p>\n<h4>Helpful Observation to add 1 to linked list<\/h4>\n<ul>\n<li>Since we do an arithmetic operation starting from the one&#8217;s place digit and then move towards the tenth place digit, hundredth place digit and so on, therefore we need to get hold of the one&#8217;s place digit to add 1 to linked list.<\/li>\n<li>Now the one&#8217;s place digit is the last digit of our number given in the form of a linked list. But we cannot move backside in the linked list, as only the next pointer is given.<\/li>\n<li>So we can reverse the linked list and do operations starting from the first node of the reversed linked list.<\/li>\n<li>So, our original linked list will be reversed and thus we can now simply add 1 to the head of the linked list (maintaining carry). Finally, we will reverse the list again and will output it as our final linked list.<\/li>\n<\/ul>\n<h3>Approach and Algorithm to add 1 to linked list<\/h3>\n<ul>\n<li>Reverse the given Linked List. For example, the given linked list is 1 \u2192 9 \u2192 9 \u2192 9 \u2192 9, after reversing it will become 9 \u2192 9 \u2192 9 \u2192 9 \u2192 1.<\/li>\n<li>Start the linked list traversal, take 2 variable <strong>sum<\/strong> and <strong>carry<\/strong>.<\/li>\n<li>The variable <strong>sum<\/strong> will hold the value at a particular place (node) and <strong>carry<\/strong> will take forward the <strong>carry<\/strong> from the previous <strong>sum<\/strong> given by <strong>(sum\/10)<\/strong>.<\/li>\n<li>Now, since we need to add 1 to linked list, the sum can be either equal than 10 or less than 10.<\/li>\n<li>Therefore, <strong>carry will be 1 for a sum greater than 10<\/strong> and <strong>0 for a sum less than 10.<\/strong><\/li>\n<li>Keep moving forward in the list and store the appropriate value in the resultant list node given by <strong>sum%10<\/strong>.<\/li>\n<li>At last, again reverse the linked list to get the output in the correct format.<\/li>\n<\/ul>\n<h3>Dry Run to add 1 to linked list<\/h3>\n<p><strong>For addOne Function<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Add-1-To-Number-Represented-As-Linked-List-2.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Add-1-To-Number-Represented-As-Linked-List-3.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation to add 1 to linked list<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4439 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_4439 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_4439 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4439 .wpsm_nav-tabs > li.active > a, #tab_container_4439 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4439 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_4439 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_4439 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4439 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4439 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4439 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4439 .wpsm_nav-tabs > li > a:hover , #tab_container_4439 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_4439 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_4439 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_4439 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_4439 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_4439 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_4439 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_4439 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4439 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_4439 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4439 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_4439 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_4439\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4439\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4439_1\" aria-controls=\"tabs_desc_4439_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4439_2\" aria-controls=\"tabs_desc_4439_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4439_3\" aria-controls=\"tabs_desc_4439_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4439_4\" aria-controls=\"tabs_desc_4439_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_4439\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_4439_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n\r\nstruct Node\r\n{\r\n    int data;\r\n    struct Node* next;\r\n    struct Node* prev;\r\n};\r\n \r\n\/* Function to create a new node with given data *\/\r\nstruct Node *newNode(int data)\r\n{\r\n    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));\r\n    new_node-&gt;data = data;\r\n    new_node-&gt;next = NULL;\r\n    return new_node;\r\n}\r\n \r\n\/* Function to reverse the linked list *\/\r\nstruct Node *reverse(struct Node *head)\r\n{\r\n    struct Node * prev   = NULL;\r\n    struct Node * current = head;\r\n    struct Node * next;\r\n    while (current != NULL)\r\n    {\r\n        next  = current-&gt;next;\r\n        current-&gt;next = prev;\r\n        prev = current;\r\n        current = next;\r\n    }\r\n    return prev;\r\n}\r\n \r\n\/* Adds one to a linked lists and return the head\r\n   node of resultant list *\/\r\nstruct Node *addOneUtil(struct Node *head)\r\n{\r\n    \/\/ res is head node of the resultant list\r\n    struct Node* res = head;\r\n    struct Node *temp;\r\n    struct Node* prev = NULL;\r\n \r\n    int carry = 1, sum;\r\n \r\n    while (head != NULL) \/\/while both lists exist\r\n    {\r\n        \/\/ Calculate value of next digit in resultant list.\r\n        \/\/ The next digit is sum of following things\r\n        \/\/ (i) Carry\r\n        \/\/ (ii) Next digit of head list (if there is a\r\n        \/\/     next digit)\r\n        sum = carry + head-&gt;data;\r\n \r\n        \/\/ update carry for next calculation\r\n        carry = (sum &gt;= 10)? 1 : 0;\r\n \r\n        \/\/ update sum if it is greater than 10\r\n        sum = sum % 10;\r\n \r\n        \/\/ Create a new node with sum as data\r\n        head-&gt;data = sum;\r\n \r\n        \/\/ Move head and second pointers to next nodes\r\n        temp = head;\r\n        head = head-&gt;next;\r\n    }\r\n \r\n    \/\/ if some carry is still there, add a new node to\r\n    \/\/ result list.\r\n    if (carry &gt; 0)\r\n        temp-&gt;next = newNode(carry);\r\n \r\n    \/\/ return head of the resultant list\r\n    return res;\r\n}\r\n \r\n\/\/ This function mainly uses addOneUtil().\r\nstruct Node* addOne(struct Node *head)\r\n{\r\n    \/\/ Reverse linked list\r\n    head = reverse(head);\r\n \r\n    \/\/ Add one from left to right of reversed\r\n    \/\/ list\r\n    head = addOneUtil(head);\r\n \r\n    \/\/ Reverse the modified list\r\n    return reverse(head);\r\n}\r\n \r\n\/\/ A utility function to print a linked list\r\nvoid printList(struct Node *node)\r\n{\r\n    while (node != NULL)\r\n    {\r\n        printf(&quot;%d&quot;, node-&gt;data);\r\n        node = node-&gt;next;\r\n    }\r\n    printf(&quot;&#92;n&quot;);\r\n}\r\n \r\n\/* Driver program to test above function *\/\r\nint main(void)\r\n{\r\n    struct Node *head = newNode(1);\r\n    head-&gt;next = newNode(9);\r\n    head-&gt;next-&gt;next = newNode(9);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(9);\r\n \r\n    printf(&quot;List is &quot;);\r\n    printList(head);\r\n \r\n    head = addOne(head);\r\n \r\n    printf(&quot;&#92;nResultant list is &quot;);\r\n    printList(head);\r\n \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4439_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;bits stdc++.h=&quot;&quot;&gt;\r\nusing namespace std;\r\nclass Node\r\n{\r\npublic:\r\n    int data;\r\n    Node *next;\r\n\r\n    Node(int val) {\r\n        data = val;\r\n        next = NULL;\r\n    }\r\n};\r\nNode* addOne(Node* head_ref)\r\n{\r\n    Node* res = head_ref; \/\/ res will point to the head of the resultant list.\r\n\r\n\r\n    int carry = 1 , sum;\/\/Why carry is taken as 1 ? =&gt; because we need to add 1\r\n\r\n    Node* prev; \/\/ prev pointer is necessary for the last node, if at last carry is there, we need to add a new Node with value carry.\r\n    \/\/ (Take example of 9-&gt;9-&gt;9-&gt;9)\r\n\r\n    while (head_ref != NULL) {\r\n        sum = carry + head_ref-&gt;data;\r\n\r\n        carry = (sum &gt;= 10) ? 1 : 0;\r\n\r\n        head_ref-&gt;data = (sum % 10);\r\n\r\n        prev = head_ref;\r\n        head_ref = head_ref-&gt;next;\r\n\r\n    }\r\n    if (carry &gt; 0) {\r\n        prev-&gt;next = new Node(carry);\r\n    }\r\n\r\n    return res;\r\n}\r\nNode*  reverseList(Node* head_ref) {\r\n    Node* curr = head_ref;\r\n    Node* prev = NULL, *next;\r\n\r\n    while (curr != nullptr) {\r\n        next = curr-&gt;next;\r\n        curr-&gt;next = prev;\r\n\r\n        prev = curr;\r\n        curr = next;\r\n    }\r\n\r\n    return prev;\r\n}\r\nNode* addOneToList(Node* head_ref)\r\n{\r\n    head_ref = reverseList(head_ref);\r\n\r\n    head_ref = addOne(head_ref);\r\n\r\n    head_ref = reverseList(head_ref);\r\n\r\n    return head_ref;\r\n}\r\n\r\nvoid printList(Node *node)\r\n{\r\n    while (node != NULL)\r\n    {\r\n        cout &lt;&lt; node-&gt;data &lt;&lt; &quot; &quot;;\r\n        node = node-&gt;next;\r\n    }\r\n}\r\nint main()\r\n{\r\n    Node* head = new Node(1);\r\n    head-&gt;next = new Node(9);\r\n    head-&gt;next-&gt;next = new Node(9);\r\n    head-&gt;next-&gt;next-&gt;next = new Node(9);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = new Node(9);\r\n\r\n    head = addOneToList(head);\r\n    printList(head);\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4439_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass CarryOne {\r\n \r\n    \/* Linked list node *\/\r\n    static class Node\r\n    {\r\n        int data;\r\n        Node next;\r\n    }\r\n \r\n    \/* Function to create a new node with given data *\/\r\n    static Node newNode(int data)\r\n    {\r\n        Node new_node = new Node();\r\n        new_node.data = data;\r\n        new_node.next = null;\r\n        return new_node;\r\n    }\r\n    static int addWithCarry(Node head)\r\n    {\r\n        if (head == null)\r\n            return 1;\r\n \r\n        \/\/ Add carry returned be next node call\r\n        int res = head.data + addWithCarry(head.next);\r\n \r\n        \/\/ Update data and return new carry\r\n        head.data = (res) % 10;\r\n        return (res) \/ 10;\r\n    }\r\n \r\n    \/\/ This function mainly uses addWithCarry().\r\n    static Node addOne(Node head)\r\n    {\r\n        int carry = addWithCarry(head);\r\n \r\n        \/\/ If there is carry after processing all nodes,\r\n        \/\/ then we need to add a new node to linked list\r\n        if (carry &gt; 0)\r\n        {\r\n            Node newNode = newNode(carry);\r\n            newNode.next = head;\r\n            return newNode; \/\/ New node becomes head now\r\n        }\r\n \r\n        return head;\r\n    }\r\n \r\n    \/\/ A utility function to print a linked list\r\n    static void printList(Node node)\r\n    {\r\n        while (node != null)\r\n        {\r\n            System.out.print(node.data);\r\n            node = node.next;\r\n        }\r\n        System.out.println();\r\n    }\r\n \r\n    \/* Driver code *\/\r\n    public static void main(String[] args)\r\n    {\r\n        Node head = newNode(1);\r\n        head.next = newNode(2);\r\n        head.next.next = newNode(2);\r\n\r\n        System.out.print(&quot;List is &quot;);\r\n        printList(head);\r\n \r\n        head = addOne(head);\r\n        System.out.println();\r\n        System.out.print(&quot;Resultant list is &quot;);\r\n        printList(head);\r\n    }\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4439_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node:\r\n\tdef __init__(self, data):\r\n\t\tself.data = data\r\n\t\tself.next = None\r\n\r\ndef newNode(data):\r\n\treturn Node(data)\r\n\r\ndef reverseList(head):\r\n\tif not head:\r\n\t\treturn\r\n\tcurNode = head\r\n\tprevNode = head\r\n\tnextNode = head.next\r\n\tcurNode.next = None\r\n\r\n\twhile(nextNode):\r\n\t\tcurNode = nextNode\r\n\t\tnextNode = nextNode.next\r\n\t\tcurNode.next = prevNode\r\n\t\tprevNode = curNode\r\n\r\n\treturn curNode\r\n\r\ndef addOne(head):\r\n\r\n\thead = reverseList(head)\r\n\tk = head\r\n\tcarry = 0\r\n\tprev = None\r\n\thead.data += 1\r\n\r\n\twhile(head != None) and (head.data &gt; 9 or carry &gt; 0):\r\n\t\tprev = head\r\n\t\thead.data += carry\r\n\t\tcarry = head.data \/\/ 10\r\n\t\thead.data = head.data % 10\r\n\t\thead = head.next\r\n\r\n\tif carry &gt; 0:\r\n\t\tprev.next = Node(carry)\r\n\treturn reverseList(k)\r\n\r\ndef printList(head):\r\n\tif not head:\r\n\t\treturn\r\n\twhile(head):\r\n\t\tprint(&quot;{}&quot;.format(head.data), end=&quot; &quot;)\r\n\t\thead = head.next\r\n\r\n\r\nif __name__ == '__main__':\r\n\thead = newNode(1)\r\n\thead.next = newNode(9)\r\n\thead.next.next = newNode(9)\r\n\thead.next.next.next = newNode(9)\r\n\thead = addOne(head)\r\n\tprintList(head)\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_4439 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_4439 a\"),jQuery(\"#tab-content_4439\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<h4>Output<\/h4>\n<p>2 \u2192 0 \u2192 0 \u2192 0 \u2192 0<\/p>\n<p><strong>Time Complexity:<\/strong> O(n), Overall we are doing a total of 3 passes, 2 for reversing and 1 while adding, where n is the number of nodes in the given LinkedList.<br \/>\n<strong>Space complexity:<\/strong> O(1), constant space complexity, as no extra space is used.<\/p>\n<h4>Can we Do Any Better ?<\/h4>\n<ul>\n<li>Now we have seen that for the above approach to work to add 1 to a number represented as linked list, we need to reverse the list twice. So we are doing two extra traversals worth O(N) complexity.<\/li>\n<li>However, if we observe carefully, we will see that it is the digit 9 which makes all the changes, for all other digits we just need to increment by 1. But in the case of 9 carry comes into the picture and modifies the whole list.<\/li>\n<li>So, we can try targeting the digit 9 itself. We don&#8217;t need to reverse the linked list.<\/li>\n<\/ul>\n<h3>Approach and Algorithm to add 1 to linked list <\/h3>\n<p>We will find the last node which is not 9. Now for this, there can be 3 scenarios to add q to linked list:<\/p>\n<ul>\n<li><strong>Case1:<\/strong> If there exists no node which is not 9, it means every node is 9. For example 9-&gt;9-&gt;9-&gt;9. In such a case we will make every node value equal to 0 and add a new node with value 1 at the front of the list.<\/li>\n<li><strong>Case2:<\/strong> If the last node of the list is not 9, then simply increment the value of the last node by 1.<\/li>\n<li><strong>Case3:<\/strong> If the node which is not 9 is somewhere in the middle, and after that, every node is 9. For example 1299, then in such cases increment the last non-nine node by 1 and make all nodes right to it as zero. So 1299 becomes 1300.<\/li>\n<\/ul>\n<p>We will take 2 pointers, one for storing the last node which is not 9, other for traversing the list. Then handle each case independently.<\/p>\n<h3>Dry Run to add 1 to linked list<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Add-1-To-Number-Represented-As-Linked-List-4.png\" alt=\"\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes.com\/blog\/wp-content\/uploads\/2021\/08\/Add-1-To-Number-Represented-As-Linked-List-4-2.png\" alt=\"\" \/><\/p>\n<h3>Code Implementation to add 1 to linked list<\/h3>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_4440 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_4440 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_4440 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_4440 .wpsm_nav-tabs > li.active > a, #tab_container_4440 .wpsm_nav-tabs > li.active > a:hover, #tab_container_4440 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_4440 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_4440 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_4440 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_4440 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_4440 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_4440 .wpsm_nav-tabs > li > a:hover , #tab_container_4440 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_4440 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_4440 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_4440 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_4440 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_4440 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_4440 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_4440 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4440 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_4440 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_4440 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_4440 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_4440\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_4440\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4440_1\" aria-controls=\"tabs_desc_4440_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4440_2\" aria-controls=\"tabs_desc_4440_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4440_3\" aria-controls=\"tabs_desc_4440_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_4440_4\" aria-controls=\"tabs_desc_4440_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_4440\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_4440_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n\r\nstruct Node {\r\n    int data;\r\n    struct Node* next;\r\n};\r\n \r\n\/* Function to create a new node with given data *\/\r\nstruct Node* newNode(int data)\r\n{\r\n    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));;\r\n    new_node-&gt;data = data;\r\n    new_node-&gt;next = NULL;\r\n    return new_node;\r\n}\r\n\r\nint addWithCarry(struct Node* head)\r\n{\r\n    \/\/ If linked list is empty, then\r\n    \/\/ return carry\r\n    if (head == NULL)\r\n        return 1;\r\n \r\n    \/\/ Add carry returned be next node call\r\n    int res = head-&gt;data + addWithCarry(head-&gt;next);\r\n \r\n    \/\/ Update data and return new carry\r\n    head-&gt;data = (res) % 10;\r\n    return (res) \/ 10;\r\n}\r\nstruct Node* addOne(struct Node* head)\r\n{\r\n    \/\/ Add 1 to linked list from end to beginning\r\n    int carry = addWithCarry(head);\r\n \r\n    \/\/ If there is carry after processing all nodes,\r\n    \/\/ then we need to add a new node to linked list\r\n    if (carry) {\r\n       struct Node* newNode  = (struct Node*)malloc(sizeof(struct Node));\r\n        newNode-&gt;data = carry;\r\n        newNode-&gt;next = head;\r\n        return newNode; \/\/ New node becomes head now\r\n    }\r\n \r\n    return head;\r\n}\r\nvoid printList(struct Node* node)\r\n{\r\n    while (node != NULL) {\r\n        printf(&quot;%d&quot;, node-&gt;data);\r\n        node = node-&gt;next;\r\n    }\r\n    printf(&quot;&#92;n&quot;);\r\n}\r\n \r\n\/* Driver code *\/\r\nint main(void)\r\n{\r\n    struct Node* head = newNode(1);\r\n    head-&gt;next = newNode(9);\r\n    head-&gt;next-&gt;next = newNode(9);\r\n    head-&gt;next-&gt;next-&gt;next = newNode(9);\r\n \r\n    printf(&quot;List is &quot;);\r\n    printList(head);\r\n \r\n    head = addOne(head);\r\n \r\n    printf(&quot;&#92;nResultant list is &quot;);\r\n    printList(head);\r\n \r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4440_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;bits stdc++.h=&quot;&quot;&gt;\r\nusing namespace std;\r\nclass Node\r\n{\r\npublic:\r\n    int data;\r\n    Node *next;\r\n\r\n    Node(int val) {\r\n        data = val;\r\n        next = NULL;\r\n    }\r\n};\r\nvoid printList(Node *node)\r\n{\r\n    while (node != NULL)\r\n    {\r\n        cout &lt;&lt; node-&gt;data &lt;&lt; &quot; &quot;;\r\n        node = node-&gt;next;\r\n    }\r\n}\r\nNode* addOneToList(Node* head)\r\n{\r\n    Node* last = NULL , *current_node = head;\r\n\r\n    \/\/Through this loop we will find the last non-nine node.\r\n    while (current_node-&gt;next != NULL)\r\n    {\r\n        if (current_node-&gt;data != 9) {\r\n            last = current_node;\r\n        }\r\n        current_node = current_node-&gt;next;\r\n    }\r\n\r\n\r\n    \/\/This loop will run till the second last node, so for the last node we will check using if condition.\r\n    \/\/If the last node itself is not 9. Then simply increment the value by 1 and return the head.\r\n    if (current_node-&gt;data != 9) {\r\n        current_node-&gt;data++;\r\n        return head;\r\n    }\r\n\r\n    \/\/After loop if last is still NULL, that means there is no value which is not 9.\r\n    \/\/For example 9-&gt;9-&gt;9-&gt;9\r\n    if (last == NULL) {\r\n        \/\/1 extra node will be needed.\r\n        last = new Node(1);\r\n        last-&gt;next = head;\r\n        head = last;\r\n\r\n        \/\/then make every node 0\r\n        last = last-&gt;next;\r\n        while (last != NULL) {\r\n            last-&gt;data = 0;\r\n            last = last-&gt;next;\r\n        }\r\n        return head;\r\n    }\r\n\r\n    \/\/Last case =&gt; when the non-nine node is somewhere in middle;\r\n    \/\/ we will increment the value and make everything right to it as 0\r\n    last-&gt;data++;\r\n    last = last-&gt;next;\r\n    while (last != NULL) {\r\n        last-&gt;data = 0;\r\n        last = last-&gt;next;\r\n    }\r\n    return head;\r\n}\r\nint main()\r\n{\r\n    Node* head = new Node(1);\r\n    head-&gt;next = new Node(9);\r\n    head-&gt;next-&gt;next = new Node(9);\r\n    head-&gt;next-&gt;next-&gt;next = new Node(9);\r\n    head-&gt;next-&gt;next-&gt;next-&gt;next = new Node(9);\r\n\r\n    head = addOneToList(head);\r\n    printList(head);\r\n    return 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4440_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass CarryOne{\r\n \r\n    static class Node\r\n    {\r\n        int data;\r\n        Node next;\r\n    }\r\n    static Node newNode(int data)\r\n    {\r\n        Node new_node = new Node();\r\n        new_node.data = data;\r\n        new_node.next = null;\r\n        return new_node;\r\n    }\r\n     \r\n    static Node addOne(Node head)\r\n    {\r\n         \r\n        Node last = null ;\r\n        Node current_node = head;\r\n        \/\/Through this loop we will find the last non-nine node.\r\n        while (current_node.next != null)\r\n        {\r\n            if (current_node.data != 9) {\r\n                last = current_node;\r\n            }\r\n            current_node = current_node.next;\r\n        }\r\n        \/\/This loop will run till the second last node, so for the last node we will check using if condition.\r\n        \/\/If the last node itself is not 9. Then simply increment the value by 1 and return the head.\r\n        if (current_node.data != 9) {\r\n            current_node.data=current_node.data+1;\r\n            return head;\r\n        }\r\n        \/\/After loop if last is still NULL, that means there is no value which is not 9.\r\n        \/\/For example 9-&gt;9-&gt;9-&gt;9\r\n        if (last == null) {\r\n            \/\/1 extra node will be needed.\r\n            last = newNode(1);\r\n            last.next = head;\r\n            head = last;\r\n            \/\/then make every node 0\r\n            last = last.next;\r\n            while (last != null) {\r\n                last.data = 0;\r\n                last = last.next;\r\n            }\r\n            return head;\r\n        }\r\n        \/\/Last case =&gt; when the non-nine node is somewhere in middle;\r\n        \/\/ we will increment the value and make everything right to it as 0\r\n        last.data=last.data+1;\r\n        last = last.next;\r\n        while (last != null) {\r\n            last.data = 0;\r\n            last = last.next;\r\n        }\r\n        return head;\r\n        \r\n    }\r\n     \r\n    \/\/ A utility function to print a linked list\r\n    static void printList(Node node)\r\n    {\r\n        while (node != null)\r\n        {\r\n            System.out.print(node.data);\r\n            node = node.next;\r\n        }\r\n        System.out.println();\r\n    }\r\n     \r\n    \/\/ Driver code\r\n    public static void main(String[] args)\r\n    {\r\n        Node head = newNode(1);\r\n        head.next = newNode(2);\r\n        head.next.next = newNode(2);\r\n     \r\n        System.out.print(&quot;List is &quot;);\r\n        printList(head);\r\n     \r\n        head = addOne(head);\r\n        System.out.println();\r\n        System.out.print(&quot;Resultant list is &quot;);\r\n        printList(head);\r\n    }\r\n    }\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_4440_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node:\r\n\tdef __init__(self, data):\r\n\t\tself.data = data\r\n\t\tself.next = None\r\n\r\ndef newNode(data):\r\n\treturn Node(data)\r\n\r\ndef printList(head):\r\n\tif not head:\r\n\t\treturn\r\n\twhile(head):\r\n\t\tprint(&quot;{}&quot;.format(head.data), end=&quot; &quot;)\r\n\t\thead = head.next\r\n\r\ndef addOneToList(head):\r\n\tlast = None\r\n\tcurrent_node = head\r\n\r\n\t# Through this loop we will find the last non-nine node\r\n\twhile current_node.next:\r\n\r\n\t\tif current_node.data != 9:\r\n\t\t\tlast = current_node\r\n\r\n\t\tcurrent_node = current_node.next\r\n\r\n\t# This loop will run till the second last node, so for the last node we will check using if condition.\r\n\t# If the last node itself is not 9. Then simply increment the value by 1 and return the head.\r\n\tif current_node.data != 9:\r\n\t\tcurrent_data.data += 1\r\n\t\treturn head\r\n\r\n\t# After loop if last is still NULL, that means there is no value which is not 9\r\n\t# For example 9-&gt;9-&gt;9-&gt;9\r\n\tif last == None:\r\n\t\t# 1 extra node will be needed.\r\n\t\tlast = newNode(1)\r\n\t\tlast.next = head\r\n\t\thead = last\r\n\r\n\t\t# then make every node 0\r\n\t\tlast = last.next\r\n\t\twhile last != None:\r\n\t\t\tlast.data = 0\r\n\t\t\tlast = last.next\r\n\r\n\t\treturn head\r\n\r\n\t# Last case =&gt; when the non-nine node is somewhere in middle;\r\n\t# we will increment the value and make everything right to it as 0\r\n\tlast.data += 1\r\n\tlast = last.next\r\n\twhile last:\r\n\t\tlast.data = 0\r\n\t\tlast = last.next\r\n\treturn head\r\n\r\n\r\nif __name__ == '__main__':\r\n\thead = newNode(1)\r\n\thead.next = newNode(9)\r\n\thead.next.next = newNode(9)\r\n\thead.next.next.next = newNode(9)\r\n\thead.next.next.next.next = newNode(9)\r\n\thead = addOneToList(head)\r\n\tprintList(head)\r\n\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_4440 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_4440 a\"),jQuery(\"#tab-content_4440\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<h4>Output<\/h4>\n<p>2 \u2192 0 \u2192 0 \u2192 0 \u2192 0<\/p>\n<p><strong>Time Complexity:<\/strong> O(n) only single pass, where n is the number of nodes in the given LinkedList.<br \/>\n[forminator_quiz id=&#8221;4441&#8243;]<\/p>\n<p>In this article we have explained how to add 1 to a number represented as linked list. We have explained different approaches with a pictorial dry run and code implementation with time and space complexities as well. To learn more about the linked list you can follow this link <a href=\"https:\/\/mycode.prepbytes.com\/interview-coding\/practice\/linked-list\">Linked List<\/a>.<\/p>\n<p>Footer<\/p>\n<h2>FAQs to add 1 to linked list<\/h2>\n<p><ol>\n<strong><\/p>\n<li>What is the time complexity to count the number of elements in a linked list?<\/li>\n<p><\/strong><br \/>\nTo count the number of elements, you have to traverse through a linked list and it will take O(n) time.<br \/>\n<strong><\/p>\n<li>What are the basic operations that can be performed in a linked list?<\/li>\n<p><\/strong><br \/>\nWe can do traversal, insertion, deletion, searching, updating, sorting and  merging in the linked list.<br \/>\n<strong><\/p>\n<li>Which operation is not supported by the linked list?<\/li>\n<p><\/strong><br \/>\nCirculate is not supported by a linked list as a linked list is a linear collection of data elements whose order is not given by their physical placement in memory.<\/ol><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In the article, we will learn to add 1 to a number represented as linked list.As we know a linked list is a linear data structure. In a linked list, the elements are linked using pointers and each node points to the next node. Let&#8217;s try to understand how we can 1 to a [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-4438","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Add 1 To Number Represented As Linked List | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Given a number represented in the form of a Linked List, you have to add 1 to it. This blog discuss the problem when we are given a number in the form of a linked list and we need to add 1 to it.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Add 1 To Number Represented As Linked List | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Given a number represented in the form of a Linked List, you have to add 1 to it. This blog discuss the problem when we are given a number in the form of a linked list and we need to add 1 to it.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-26T11:18:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-08T07:59:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185498768-Add%20to%201%20Number_Img1.png\" \/>\n<meta name=\"author\" content=\"PrepBytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"PrepBytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/\"},\"author\":{\"name\":\"PrepBytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\"},\"headline\":\"Add 1 To Number Represented As Linked List\",\"datePublished\":\"2021-08-26T11:18:58+00:00\",\"dateModified\":\"2022-11-08T07:59:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/\"},\"wordCount\":1103,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185498768-Add%20to%201%20Number_Img1.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/\",\"name\":\"Add 1 To Number Represented As Linked List | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185498768-Add%20to%201%20Number_Img1.png\",\"datePublished\":\"2021-08-26T11:18:58+00:00\",\"dateModified\":\"2022-11-08T07:59:27+00:00\",\"description\":\"Given a number represented in the form of a Linked List, you have to add 1 to it. This blog discuss the problem when we are given a number in the form of a linked list and we need to add 1 to it.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185498768-Add%20to%201%20Number_Img1.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185498768-Add%20to%201%20Number_Img1.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linked list articles\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linked-list\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Add 1 To Number Represented As Linked List\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e\",\"name\":\"PrepBytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g\",\"caption\":\"PrepBytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Add 1 To Number Represented As Linked List | Linked List | Prepbytes","description":"Given a number represented in the form of a Linked List, you have to add 1 to it. This blog discuss the problem when we are given a number in the form of a linked list and we need to add 1 to it.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Add 1 To Number Represented As Linked List | Linked List | Prepbytes","og_description":"Given a number represented in the form of a Linked List, you have to add 1 to it. This blog discuss the problem when we are given a number in the form of a linked list and we need to add 1 to it.","og_url":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2021-08-26T11:18:58+00:00","article_modified_time":"2022-11-08T07:59:27+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185498768-Add%20to%201%20Number_Img1.png","type":"","width":"","height":""}],"author":"PrepBytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"PrepBytes","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/"},"author":{"name":"PrepBytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e"},"headline":"Add 1 To Number Represented As Linked List","datePublished":"2021-08-26T11:18:58+00:00","dateModified":"2022-11-08T07:59:27+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/"},"wordCount":1103,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185498768-Add%20to%201%20Number_Img1.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/","name":"Add 1 To Number Represented As Linked List | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185498768-Add%20to%201%20Number_Img1.png","datePublished":"2021-08-26T11:18:58+00:00","dateModified":"2022-11-08T07:59:27+00:00","description":"Given a number represented in the form of a Linked List, you have to add 1 to it. This blog discuss the problem when we are given a number in the form of a linked list and we need to add 1 to it.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185498768-Add%20to%201%20Number_Img1.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645185498768-Add%20to%201%20Number_Img1.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/add-1-to-number-represented-as-linked-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linked list articles","item":"https:\/\/prepbytes.com\/blog\/category\/linked-list\/"},{"@type":"ListItem","position":3,"name":"Add 1 To Number Represented As Linked List"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/39fcf072e04987f16796546f2ca83c2e","name":"PrepBytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/850669d326db1e1531f04db0c63145d941c2a26792aaeee226a9e6675b0ac698?s=96&d=mm&r=g","caption":"PrepBytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/prepbytes\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=4438"}],"version-history":[{"count":10,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4438\/revisions"}],"predecessor-version":[{"id":10369,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/4438\/revisions\/10369"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=4438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=4438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=4438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}