Add 1 to a number represented as linked list | Practice | GeeksforGeeks

A number N is represented in Linked List such that each digit corresponds to a node in linked list. You need to add 1 to it.
Example 1:
Input:
LinkedList: 4->5->6
Output: 457 

Example 2:
Input:
LinkedList: 1->2->3
Output: 124 
Your Task:
Your task is to complete the function addOne() which takes the head of the linked list as the only argument and returns the head of the modified linked list. The driver code prints the number.
Note: The head represents the left-most digit of the number.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 101000
+3
yuktimachaurasiya5 days ago
class Solution{   public:   Node* addOne(Node *head)    {       if(!head) return head;       if(head->next==NULL){           head->data+=1;           return head;       }       Node *temp=addOne(head->next);       if(temp->data==10){           head->data+=1;           temp->data=0;       }       return head;   }}; 
0
adarsh471115 days ago
Java solution
class Solution
{
    public static Node addOne(Node head) 
    { 
        head=reverse(head);
        Node cur=head;
        int carry=1;  // stting carry 1 from start  to add 1
        
        while(cur!=null)
        {
            int sum=cur.data+carry;
            carry=sum/10;
            cur.data=sum%10;
            
            if(cur.next==null)
            break;
            
            cur=cur.next;
        }
        if(carry==1)
        {
            Node temp=new Node(1);
            cur.next=temp;
            cur=temp;
        }
        return head=reverse(head);
    }
    
    static Node reverse(Node head)
    {
        Node cur=head;
        Node prev=null;
        Node next;
        
        while(cur!=null)
        {
            next=cur.next;
            cur.next=prev;
            prev=cur;
            cur=next;
        }
        return prev;
    }
}

0
adarsh47111
This comment was deleted.
0
jainmuskan5655 days ago
 Node* reverse(Node* head){
        Node* prev= NULL;
        Node *curr= head;
        while(curr){
            Node* temp= curr->next;
            curr->next= prev;
            prev= curr;
            curr= temp;
        }
        // as it is the first node
        return prev;
    }
    Node* addOne(Node *head) 
    {
        // Your Code here
        // return head of list after adding one
        // reverse the linked list and check whether the element is 9
        Node * last= reverse(head);
        Node *curr= last;
        bool f= true;
        while(curr && f){
            if(curr->next==NULL && curr->data ==9){
                curr->data= 1;
                // case where 999 --> 1000
                Node *temp= new Node(0);
                temp->next= last;
                last= temp;
            }
            else if(curr->data==9){
                curr->data=0;
            }
            else{
                curr->data= curr->data+1;
                f= false;
                // to break the loop when there is no need to add more
            }
            curr=curr->next;
        }
        last= reverse(last);
        return last;
    }
0
shakshamkaushik1
This comment was deleted.
0
shakshamkaushik16 days ago
   public static Node addOne(Node head)    {        if(head == null){           return new Node(1);       }       head = reverse (head);       Node curr = head;       int carry = 1;       while(carry > 0){           int sum = curr.data + carry;           curr.data = sum%10;           carry = sum/10;           if(curr.next == null){               break;           }           curr = curr.next;       }           if(carry >0){               curr.next = new Node(carry);           }                                   head = reverse (head);       return head;         }   public static Node reverse (Node head){       Node curr = head;       Node prev = null;       while(curr!=null){           Node temp = curr.next;           curr.next = prev;           prev = curr;           curr = temp;       }       return prev;   } 
0
dev1711 week ago
Node* reverse(Node* k){
    Node *prev=NULL;
    while(k){
        Node*  next=k->next;
        k->next=prev;
        prev=k;
        k=next;
    }
    return prev;
}

    Node* addOne(Node *head) 
    {
       head=reverse(head);
        Node *k=head,*prev=k;
        int carry=1;
            while(carry!=0 && k){
                int val=k->data+carry;
                k->data=val%10;

                carry=val/10;
                prev=k;
                k=k->next;
            }
            if(carry>0){
                prev->next=new Node(carry);
            }
        
        return reverse(head);
        
    }
0
kimbbang1 week ago
Add 1 to a number represented as linked list
https://www.geeksforgeeks.org/add-1-number-represented-linked-list/
0
mohitm15091 week ago
//Java best Sol. Time 0.5
 
public static Node reverse(Node head){       Node prev = null;       Node cur = head;       while(cur != null){           Node temp = cur.next;           cur.next = prev;           prev = cur;           cur = temp;       }       return prev;          }   public static Node addOne(Node head)    {        //code here.       if(head == null){           return new Node(1);       }       head = reverse(head);       Node cur = head;       int carry = 1;       while(carry > 0){           int sum = cur.data + carry;           cur.data = sum%10;           carry = sum/10;           if(cur.next == null)               break;           cur = cur.next;       }       if(carry > 0)           cur.next = new Node(carry);              head = reverse(head);       return head;
0
ishitavashisht20001 week ago
class Solution{   public:   Node* reverse(Node* head){   Node* previous=NULL;   Node* curent=head;   Node* nex;   while(curent!=NULL){       nex=curent->next;       curent->next=previous;       previous=curent;       curent=nex;   }   return previous;          }   public:   Node* addOne(Node *head)    { Node* temp=head->next;     int num=head->data;     while(temp!=NULL){         num=num*10+temp->data;         //num*=10;         temp=temp->next;     }     num=num+1;     cout<<num<<" ";     temp=head;     while(temp!=NULL && num>0){         int r=num%10;         //ut<<r<<" ";         temp->data=r;         //ut<<temp->data<<" ";         temp=temp->next;         num=num/10;              }          return reverse(head);     //return head;       // Your Code here       // return head of list after adding one   }};
 
don't know why it is failing for input=218729081793941999
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.