Add 1 to a number represented as linked list
PROBLEM :
A number (n) is represented in Linked List such that each digit corresponds to a node in linked list. Add 1 to it.
Input:
You have to complete the method which takes one argument: the head of the linked list. You should not read any input from stdin/console.. There are multiple test cases. For each test case, this method will be called individually
Output:
Your function should return pointer to head of the modified list.
Constraints:
1 <=T<= 50
1 <=n<= 10000
Example:
Input:
4
456
123
999
1879
Output:
457
124
1000
1880
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* Node structure
struct Node
{
int data;
struct Node* next;
}; */
// Should rearrange given linked list such that all even
// positioned Nodes are before odd positioned.
// Returns new head of linked List.
struct Node* reverse(struct Node *) ;
Node *addOne(Node *head)
{
if(head==NULL)
return head ;
int sum,rem ;
head=reverse(head) ;
Node *ptr, *temp=head ;
rem=1 ;
while(temp!=NULL)
{
sum=temp->data+rem ;
temp->data=sum%10 ;
rem=sum/10 ;
temp=temp->next ;
}
if(rem!=0)
{
temp=head ;
while(temp->next!=NULL)
temp=temp->next ;
ptr=(Node*)malloc(sizeof(Node)) ;
ptr->data=rem ;
ptr->next=NULL ;
temp->next=ptr ;
}
head=reverse(head) ;
return head ;
}
struct Node* reverse(struct Node *head)
{
if(head==NULL)
return head ;
if(head->next==NULL)
return head;
struct Node *one,*two,*third ;
one=head ;
two=one->next ;
third=two->next ;
one->next=NULL ;
two->next=one ;
while(third!=NULL)
{
one=two ;
two=third ;
third=third->next ;
two->next=one ;
}
return two ;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Better solution - without reversing, ie in single traversal)
--------------------------------------------------------------------------------
/* Node structure
struct Node
{
int data;
struct Node* next;
}; */
// Should rearrange given linked list such that all even
// positioned Nodes are before odd positioned.
// Returns new head of linked List.
Node* ADDone(Node *,int *) ;
Node *addOne(Node *head)
{
if(head==NULL)
return NULL ;
int carry=0 ;
head=ADDone(head,&carry) ;
if(carry!=0)
{
Node *temp=(Node*)malloc(sizeof(Node)) ;
temp->data=carry ;
temp->next=head ;
head=temp ;
}
return head ;
}
Node* ADDone(Node *head,int *carry)
{
if(head==NULL)
return NULL ;
Node *prev ;
prev=ADDone(head->next,&(*carry)) ;
if(prev==NULL)
{
head->data=head->data+1 ;
(*carry)=head->data/10 ;
head->data=head->data%10 ;
}
else
{
head->data=head->data+(*carry) ;
(*carry)=head->data/10 ;
head->data=head->data%10 ;
}
head->next=prev ;
return head ;
}
---------------------------------------------------------------------------------
A number (n) is represented in Linked List such that each digit corresponds to a node in linked list. Add 1 to it.
Input:
You have to complete the method which takes one argument: the head of the linked list. You should not read any input from stdin/console.. There are multiple test cases. For each test case, this method will be called individually
Output:
Your function should return pointer to head of the modified list.
Constraints:
1 <=T<= 50
1 <=n<= 10000
Example:
Input:
4
456
123
999
1879
Output:
457
124
1000
1880
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/* Node structure
struct Node
{
int data;
struct Node* next;
}; */
// Should rearrange given linked list such that all even
// positioned Nodes are before odd positioned.
// Returns new head of linked List.
struct Node* reverse(struct Node *) ;
Node *addOne(Node *head)
{
if(head==NULL)
return head ;
int sum,rem ;
head=reverse(head) ;
Node *ptr, *temp=head ;
rem=1 ;
while(temp!=NULL)
{
sum=temp->data+rem ;
temp->data=sum%10 ;
rem=sum/10 ;
temp=temp->next ;
}
if(rem!=0)
{
temp=head ;
while(temp->next!=NULL)
temp=temp->next ;
ptr=(Node*)malloc(sizeof(Node)) ;
ptr->data=rem ;
ptr->next=NULL ;
temp->next=ptr ;
}
head=reverse(head) ;
return head ;
}
struct Node* reverse(struct Node *head)
{
if(head==NULL)
return head ;
if(head->next==NULL)
return head;
struct Node *one,*two,*third ;
one=head ;
two=one->next ;
third=two->next ;
one->next=NULL ;
two->next=one ;
while(third!=NULL)
{
one=two ;
two=third ;
third=third->next ;
two->next=one ;
}
return two ;
}
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :(Better solution - without reversing, ie in single traversal)
--------------------------------------------------------------------------------
/* Node structure
struct Node
{
int data;
struct Node* next;
}; */
// Should rearrange given linked list such that all even
// positioned Nodes are before odd positioned.
// Returns new head of linked List.
Node* ADDone(Node *,int *) ;
Node *addOne(Node *head)
{
if(head==NULL)
return NULL ;
int carry=0 ;
head=ADDone(head,&carry) ;
if(carry!=0)
{
Node *temp=(Node*)malloc(sizeof(Node)) ;
temp->data=carry ;
temp->next=head ;
head=temp ;
}
return head ;
}
Node* ADDone(Node *head,int *carry)
{
if(head==NULL)
return NULL ;
Node *prev ;
prev=ADDone(head->next,&(*carry)) ;
if(prev==NULL)
{
head->data=head->data+1 ;
(*carry)=head->data/10 ;
head->data=head->data%10 ;
}
else
{
head->data=head->data+(*carry) ;
(*carry)=head->data/10 ;
head->data=head->data%10 ;
}
head->next=prev ;
return head ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment