forked from Masked-coder11/gfg-POTD
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e828d1
commit b6ba554
Showing
1 changed file
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* node for linked list: | ||
struct Node { | ||
int data; | ||
struct Node* next; | ||
Node(int x) { | ||
data = x; | ||
next = NULL; | ||
} | ||
}; | ||
*/ | ||
|
||
class Solution | ||
{ | ||
public: | ||
Node* reverse(Node* head){ | ||
if(!head || !head->next) return head; | ||
|
||
Node*prev=NULL, *temp=head, *next=head->next; | ||
while(next){ | ||
temp->next=prev; | ||
prev=temp; | ||
temp=next; | ||
next=temp->next; | ||
} | ||
temp->next=prev; | ||
|
||
return temp; | ||
} | ||
|
||
Node* addTwoNumbers(Node* l1, Node* l2) { | ||
if(!l1) return l2; | ||
if(!l2) return l1; | ||
|
||
|
||
Node* head=l1; | ||
|
||
Node* tail; | ||
|
||
int carry=0; | ||
while(l1 && l2){ | ||
l1->data += carry+l2->data; | ||
carry=l1->data/10; | ||
l1->data %= 10; | ||
|
||
tail=l1; | ||
|
||
l1=l1->next; | ||
l2=l2->next; | ||
} | ||
if(l2){ | ||
tail->next=l2; | ||
l1=l2; | ||
} | ||
while(l1){ | ||
l1->data+=carry; | ||
carry=l1->data/10; | ||
l1->data %= 10; | ||
tail=l1; | ||
l1=l1->next; | ||
} | ||
|
||
l1=tail; | ||
while(carry){ | ||
l1->next=new Node(carry); | ||
carry=l1->data/10; | ||
l1->data %= 10; | ||
l1=l1->next; | ||
} | ||
|
||
|
||
return head; | ||
|
||
|
||
} | ||
//Function to add two numbers represented by linked list. | ||
struct Node* addTwoLists(struct Node* num1, struct Node* num2) | ||
{ | ||
// code here | ||
while(num1->next && num1->data==0){ | ||
num1=num1->next; | ||
} | ||
while(num2->next && num2->data==0){ | ||
num2=num2->next; | ||
} | ||
|
||
num1=reverse(num1); | ||
num2=reverse(num2); | ||
|
||
return reverse(addTwoNumbers(num1, num2)); | ||
|
||
} | ||
}; |