-
Notifications
You must be signed in to change notification settings - Fork 0
/
addtwolinkedlist.txt
61 lines (56 loc) · 1.42 KB
/
addtwolinkedlist.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
/****************************************************************
Following is the class structure of the Node class:
class Node
{
public:
int data;
Node *next;
Node(int data)
{
this->data = data;
this->next = NULL;
}
};
*****************************************************************/
Node *addTwoNumbers(Node *head1, Node *head2)
{
// Write your code here.
Node* head =new Node(-1);
Node* cur=head;
int sum=0,carry=0;
while(head1!=NULL && head2!=NULL){
sum = head1->data +head2->data +carry;
Node * temp =new Node(sum%10);
if(sum > 9)
carry=sum/10;
else carry=0;
cur ->next =temp;
cur=cur->next;
head1=head1->next;
head2=head2 ->next;
}
while(head1!=NULL){
sum =head1->data +carry;
Node *t=new Node(sum%10);
if(sum > 9)carry=sum/10;
else carry=0;
cur->next=t;
cur=cur->next;
head1=head1->next;
}
while(head2!=NULL){
sum =head2->data +carry;
Node *t=new Node(sum%10);
if(sum > 9)carry=sum/10;
else carry=0;
cur->next=t;
cur=cur->next;
head2=head2->next;
}
if(carry!=0){
Node* t=new Node(carry);
cur ->next =t;
}
return head->next;
}