you have two linked list for two decimal numbers means
if there r two no..........3627809 & 234
two linked list will be
1. 3->6->2->7->8->0->9->NULL
2. 2->3->4->NULL
your output linked list will be
3->6->2->8->0->4->3->NULL
Answer -
First reverse both the lists and then add. After adding reverse the resulted linked list. Code is as follows -
if there r two no..........3627809 & 234
two linked list will be
1. 3->6->2->7->8->0->9->NULL
2. 2->3->4->NULL
your output linked list will be
3->6->2->8->0->4->3->NULL
Answer -
First reverse both the lists and then add. After adding reverse the resulted linked list. Code is as follows -
linked list p,q, result; int x=0,num; p=reverse(p); q=reverse(q); while(p!=NULL || q!=NULL) { num=p->data+q->data+x; result->data=num%10; x=num/10; p=p->link; q=q->link; } if(p!=NULL) { p->data=p->data+x; add their nodes at the back; } if(q!=NULL) { q->data=q->data+x; add their nodes at the back; } result=reverse(result);
No comments:
Post a Comment