You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807.
Example 2:
Input: l1 = [0], l2 = [0] Output: [0]
Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] Output: [8,9,9,9,0,0,0,1]
Solution :
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* l3 = NULL;
static int rem = 0;
while(l1 != NULL || l2 != NULL )
{
if(l1 != NULL && l2 != NULL)
{
int value = rem + l1->val + l2->val;
if(value > 9)
{
if(l3!=NULL)
{
ListNode* t=l3;
while(t->next != NULL)
{
cout << t->val;
t=t->next;
}
t->next = new ListNode(value % 10);
}
else
l3 = new ListNode(value % 10);
rem = value / 10;
l1 = l1->next; l2 = l2->next;
}
else
{
if(l3!=NULL)
{
ListNode* t=l3;
while(t->next != NULL)
t=t->next;
t->next = new ListNode(value);
}
else
l3 = new ListNode(value);
l1 = l1->next; l2 = l2->next;
(rem > 0) ? rem=0 : rem=rem;
}
}
if(l1 != NULL && l2 == NULL)
{
int value = rem + l1->val;
if(value > 9)
{
if(l3!=NULL)
{
ListNode* t=l3;
while(t->next != NULL)
{
cout << t->val;
t=t->next;
}
t->next = new ListNode(value % 10);
}
else
l3 = new ListNode(value % 10);
rem = value / 10;
l1 = l1->next;
}
else
{
if(l3!=NULL)
{
ListNode* t=l3;
while(t->next != NULL)
t=t->next;
t->next = new ListNode(value);
}
else
l3 = new ListNode(value);
l1 = l1->next;
(rem > 0) ? rem=0 : rem=rem;
}
}
if(l1 == NULL && l2 != NULL)
{
int value = rem + l2->val;
if(value > 9)
{
if(l3!=NULL)
{
ListNode* t=l3;
while(t->next != NULL)
{
cout << t->val;
t=t->next;
}
t->next = new ListNode(value % 10);
}
else
l3 = new ListNode(value % 10);
rem = value / 10;
l2 = l2->next;
}
else
{
if(l3!=NULL)
{
ListNode* t=l3;
while(t->next != NULL)
t=t->next;
t->next = new ListNode(value);
}
else
l3 = new ListNode(value);
l2 = l2->next;
(rem > 0) ? rem=0 : rem=rem;
}
}
}
while(rem > 0)
{
if(l3!=NULL)
{
ListNode* t=l3;
while(t->next != NULL)
t=t->next;
t->next = new ListNode(rem % 10);
}
else
l3 = new ListNode(rem % 10);
rem = rem/10;
}
return l3;
}
};
Complete Code :
// Add 2 linked list
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* head1 = NULL;
void insert1(int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = head1;
head1 = new_node;
}
struct Node* head2 = NULL;
void insert2(int new_data) {
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = head2;
head2 = new_node;
}
void display(struct Node *h) {
struct Node* ptr;
ptr = h;
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
struct Node* addTwoNumbers(struct Node* l1, struct Node* l2) {
struct Node* l3=NULL;
static int rem = 0;
while(l1 != NULL || l2 != NULL )
{
if(l1 != NULL && l2 != NULL)
{
int value = rem + l1->data + l2->data;
if(value > 9)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = (value % 10);
new_node->next = NULL;
if(l3 != NULL)
{
struct Node* t = l3;
while(t->next != NULL)
t = t->next;
t->next = new_node;
}
else
l3 = new_node;
rem = value / 10;
l1 = l1->next; l2 = l2->next;
}
else
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = (value % 10);
new_node->next = NULL;
if(l3 != NULL)
{
struct Node* t = l3;
while(t->next != NULL)
t = t->next;
t->next = new_node;
}
else
l3 = new_node;
l1 = l1->next; l2 = l2->next;
(rem > 0) ? rem=0 : rem=rem;
}
}
if(l1 != NULL && l2 == NULL)
{
int value = rem + l1->data;
if(value > 9)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = (value % 10);
new_node->next = NULL;
if(l3 != NULL)
{
struct Node* t = l3;
while(t->next != NULL)
t = t->next;
t->next = new_node;
}
else
l3 = new_node;
rem = value / 10;
l1 = l1->next;
}
else
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = (value % 10);
new_node->next = NULL;
if(l3 != NULL)
{
struct Node* t = l3;
while(t->next != NULL)
t = t->next;
t->next = new_node;
}
else
l3 = new_node;
l1 = l1->next;
(rem > 0) ? rem=0 : rem=rem;
}
}
if(l1 == NULL && l2 != NULL)
{
int value = rem + l2->data;
if(value > 9)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = (value % 10);
new_node->next = NULL;
if(l3 != NULL)
{
struct Node* t = l3;
while(t->next != NULL)
t = t->next;
t->next = new_node;
}
else
l3 = new_node;
rem = value / 10;
l2 = l2->next;
}
else
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = (value % 10);
new_node->next = NULL;
if(l3 != NULL)
{
struct Node* t = l3;
while(t->next != NULL)
t = t->next;
t->next = new_node;
}
else
l3 = new_node;
l2 = l2->next;
(rem > 0) ? rem=0 : rem=rem;
}
}
}
//cout << "rau0 " << l3->data << endl;
while(rem > 0)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = (rem % 10);
new_node->next = NULL;
if(l3 != NULL)
{
struct Node* t = l3;
while(t->next != NULL)
t = t->next;
t->next = new_node;
}
else
l3 = new_node;
rem = rem / 10;
}
return l3;
}
int main() {
insert1(119);
insert1(9);
insert1(9);
insert1(9);
insert1(9);
insert1(9);
insert2(9);
insert2(9);
insert2(9);
insert2(9);
struct Node* h3 = NULL;
h3 = addTwoNumbers(head1, head2);
display(h3);
return 0;
}