Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Add 1 to a number represented as a linked list?
The linked list representation of a number stores each digit in a separate node, where the first node contains the most significant digit and the last node contains the least significant digit. For example, the number 202345 is represented as (2→0→2→3→4→5).
To add 1 to this linked list represented number, we need to handle carry propagation from the least significant digit. If the last digit is less than 9, we simply increment it. Otherwise, we propagate the carry to the next digits.
Syntax
struct Node* addOne(struct Node* head);
Algorithm
The algorithm follows these steps −
- Reverse the linked list to access the least significant digit first
- Add 1 to the first node and handle carry propagation
- Reverse the list back to restore the original order
- Handle edge case where a new node is needed for carry
Example
Here's a complete implementation that adds 1 to a number represented as a linked list −
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* newNode(int data) {
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = data;
new_node->next = NULL;
return new_node;
}
struct Node* reverse(struct Node* head) {
struct Node* prev = NULL;
struct Node* current = head;
struct Node* next;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
struct Node* addOneUtil(struct Node* head) {
struct Node* res = head;
struct Node* temp = NULL;
int carry = 1, sum;
while (head != NULL) {
sum = carry + head->data;
carry = (sum >= 10) ? 1 : 0;
sum = sum % 10;
head->data = sum;
temp = head;
head = head->next;
}
if (carry > 0)
temp->next = newNode(carry);
return res;
}
struct Node* addOne(struct Node* head) {
head = reverse(head);
head = addOneUtil(head);
return reverse(head);
}
void printList(struct Node* head) {
while (head != NULL) {
printf("%d", head->data);
head = head->next;
}
printf("<br>");
}
int main() {
struct Node* head = newNode(1);
head->next = newNode(9);
head->next->next = newNode(9);
head->next->next->next = newNode(9);
printf("Original number: ");
printList(head);
head = addOne(head);
printf("After adding 1: ");
printList(head);
return 0;
}
Original number: 1999 After adding 1: 2000
How It Works
For the input 1999:
- Step 1: Reverse the list: 1→9→9→9 becomes 9→9→9→1
- Step 2: Add 1 to first node: 9+1=10, store 0 and carry 1
- Step 3: Continue with carry: 9+1=10, store 0 and carry 1
- Step 4: Continue: 9+1=10, store 0 and carry 1
- Step 5: Continue: 1+1=2, store 2 and carry 0
- Step 6: Reverse back: 2→0→0→0
Time and Space Complexity
| Aspect | Complexity | Explanation |
|---|---|---|
| Time Complexity | O(n) | Three passes through the list (reverse + add + reverse) |
| Space Complexity | O(1) | Only constant extra space used |
Conclusion
Adding 1 to a linked list representation of a number requires reversing the list to handle carry propagation efficiently. The algorithm handles all edge cases including numbers ending with multiple 9s and maintains O(n) time complexity with constant space usage.
