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:

Adding one to a linked list

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 list
node *add_one_no(node *head)
{
head = reverse(head);
// The result is the head node of the resultant list
node* result = head;
node *temp;
int carry = 1, sum;
while (head != NULL)
{
// Calculating the next digit in the resultant list
sum = carry + head->data;
// Updating the carry for the next calculation
carry = (sum >= 10)? 1 : 0;
// Updating the sum if it is greater than 10
sum = sum % 10;
// Creating a new node with the sum as data
head->data = sum;
// Moving the head to the next node
temp = head;
head = head->link;
}
if (carry > 0) // Add the new node, if the carry is left
temp->link = add_node(head,carry);
// Return the head of the resultant list after reversing it
result=reverse(result);
return result;
}
// The driver code
int 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 , previous and link, to keep track of the linked list.

  • Lines 7–14: We use the while loop 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 the previous node that is the head of the reversed list.

  • Lines 19–26: We create the add_one_no function to add one to the given list. We reverse the list and define two pointers, result and temp. Then, we define two variables, carry and sum, to store values.

  • Lines 28–36: In the while loop, we calculate the upcoming digit. Then, we calculate the carry. If the sum is greater than or equal to ten, we update it to one. Otherwise, we update it to zero. Also, we update the sum by taking its modulus.

  • Lines 38–44: We create a new node with the value of sum. We update the values of temp and head for the next traversal.

  • Lines 45–50: We add new_node() if some carry is left. Lastly, we reverse the linked list and return the result linked list.

Time and space complexity

The time complexity for this approach is O(n)O(n), where nn is the number of nodes in the given linked list.

The space complexity for this approach is O(1)O(1).

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved