How to add one to a number represented as a linked list
A linked list is a data structure made up of one or more nodes. Each node contains a data value and a pointer to the next node, forming a chain-like structure. These nodes are stored randomly in the system's memory, which improves its space complexity compared to the array.
Understanding the problem
The representation of a number in a linked list means each digit of a number is added as a node of the linked list. Each node corresponds to a single digit of the number. The digits are inserted in the linked list so that the most significant digit is placed at the head of the list, and the least significant digit is placed at the tail of the linked list.
Let's visualize this:
Solution
The basic approach is first to reverse the linked list. Then, we check whether or not the left-most element (which is now the head) is 9. If yes, we keep adding one carry to the remaining nodes until the carry finishes. If not, we add one to the head of reversed linked list. Now, we again reverse the modified linked list, and return the head.
Implementation
Here's the code implementation of the above solution in C++.
/* The function to reverse the linked list */node *reverse(node *head){node * previous = NULL;node * current = head;node * link;while (current != NULL){link = current->link;current->link = previous;previous = current;current = link;}return previous;}// Adding one to a linked listnode *add_one_no(node *head){head = reverse(head);// The result is the head node of the resultant listnode* result = head;node *temp;int carry = 1, sum;while (head != NULL){// Calculating the next digit in the resultant listsum = carry + head->data;// Updating the carry for the next calculationcarry = (sum >= 10)? 1 : 0;// Updating the sum if it is greater than 10sum = sum % 10;// Creating a new node with the sum as datahead->data = sum;// Moving the head to the next nodetemp = head;head = head->link;}if (carry > 0) // Add the new node, if the carry is lefttemp->link = add_node(head,carry);// Return the head of the resultant list after reversing itresult=reverse(result);return result;}// The driver codeint main(){node* list1=new node();list1 = NULL;list1=add_node(list1,2);add_node(list1,9);add_node(list1,9);add_node(list1,9);cout << "Linked List: ";print_data(list1);cout<<"\nAfter adding one: ";list1=add_one_no(list1);print_data(list1);}
Explanation
Lines 1–6: We create the
reverse()function to reverse the linked list. We define three-pointers,current,previousandlink, to keep track of the linked list.Lines 7–14: We use the
whileloop to reverse the list. We swap the values and links of the subsequent nodes until we reach the tail of the linked list. Afterward, we return thepreviousnode that is the head of the reversed list.Lines 19–26: We create the
add_one_nofunction to add one to the given list. We reverse the list and define two pointers,resultandtemp. Then, we define two variables,carryandsum, to store values.Lines 28–36: In the
whileloop, we calculate the upcoming digit. Then, we calculate thecarry. If thesumis greater than or equal to ten, we update it to one. Otherwise, we update it to zero. Also, we update thesumby taking its modulus.Lines 38–44: We create a new node with the value of
sum. We update the values oftempandheadfor the next traversal.Lines 45–50: We add
new_node()if somecarryis left. Lastly, we reverse the linked list and return theresultlinked list.
Time and space complexity
The time complexity for this approach is
The space complexity for this approach is
Free Resources