Skip to main content

Add 1 to a Number respresented by Linked List

Problem Statement:​

You are given a linked list where each element in the list is a node and have an integer data. You need to addΒ 1Β to the number formed by concatinating all the list node numbers together and return the head of the modified linked list.

Note:Β The head represents the first element of the given array.

Input:LinkedList: 4->5->6
Output:457

Explanation: 4->5->6 represents 456 and when 1 is added it becomes 457.

  • Example:



βœ… Solution 1: Recursive (Backtracking Approach)​

class Solution {
public:
int addOneUtils(Node *head) {
if(head == NULL) return 1; // Base case: initial carry = 1

int carry = addOneUtils(head->next); // Recurse to end
int sum = head->data + carry;
head->data = sum % 10;
return sum / 10; // return carry to propagate
}

Node* addOne(Node* head) {
int carry = addOneUtils(head);
if(carry) {
Node *newNode = new Node(carry);
newNode->next = head;
head = newNode;
}
return head;
}
};


βœ… Solution 2: Iterative (Reverse + Add + Reverse)​

class Solution {
public:
Node* reverse(Node *head) {
Node *prev = NULL, *curr = head;
while(curr) {
Node *nextNode = curr->next;
curr->next = prev;
prev = curr;
curr = nextNode;
}
return prev;
}

Node* addOne(Node* head) {
head = reverse(head); // Step 1: reverse the list

int carry = 1;
Node *curr = head, *prev = NULL;

while(curr && carry) {
int sum = curr->data + carry;
curr->data = sum % 10;
carry = sum / 10;
prev = curr;
curr = curr->next;
}

if(carry) {
prev->next = new Node(carry);
}

head = reverse(head); // Step 3: reverse back
return head;
}
};


πŸ“ How It Works​

Recursive:​

  • Recurse to the end of the list (least significant digit).
  • Add 1 and propagate the carry backward during the return phase.

Iterative:​

  1. Reverse the list to make addition easier (starting from LSB).
  2. Perform addition and handle carry.
  3. Reverse back to restore original order.

🧩 Key Concepts​

ConceptRecursiveIterative
Handles carry naturallyβœ… Yesβœ… Yes
Uses reverse logic❌ Noβœ… Yes
In-place updatesβœ…βœ…
Base caseReaches NULLStarts from head

⏱️ Time & Space Complexity​

ApproachTimeSpace
RecursiveO(N)O(N) recursion stack
IterativeO(N)O(1) constant space

⚠️ Edge Cases​

  • βœ… 999 β†’ 1000
  • βœ… Empty list β†’ return NULL
  • βœ… Last node becomes 10 β†’ carry added as new node

πŸ’‘ Other Approaches​

ApproachNotes
Convert to integerNot safe for large numbers
Stack-basedPush all digits, pop and add


πŸš€ Level Up Your Engineering Mindset

Never Study in Isolation. Join Binary Dose.

Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.

Discord
Official Hub

Discord Community

Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.

Join Discord Server→
⚑
Daily Knowledge Drops

Daily Visual Deep-Dives

Bite-sized system design schematics, core OS internals, and DSA intuition served right into your feeds every single day.

πŸ’‘Have handwritten notes, cheat-sheets, or want to publish a technical article?
Learn How to Contribute→