Skip to content

Add_Two_Linkedlist.cpp #657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions Linked_List/Add-Two-linkedlist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#include <bits/stdc++. h>
using namespace std;
class Node
{
public:
int data;
Node *next;

Node(int val)
{
this->data = val;
this->next = NULL;
}

~Node()
{
int value = this->data;
if (this->next != NULL)
{
delete next;
this->next = NULL;
}
cout << "Memory is free for the node with data" << value << endl;
}
};

void insertAtHead(Node *&head, int data)
{
Node *temp = head;
Node *newNode = new Node(data);
if (head == NULL)
{
head = newNode;
}
newNode->next = head;
head = newNode;
}

void insertAtTail(Node *&head, int d)
{
// new node create
if (head == NULL)
{
insertAtHead(head, d);
}
Node *temp = new Node(d);
head->next = temp;
head = temp;
}

void display(Node *&head)
{
if (head == NULL)
{
cout << "List empty" << endl;
return;
}
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << "-> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

Node *reverse(Node *head)
{
Node *curr = head;
Node *Next = NULL;
Node *prev = NULL;
while (curr != NULL)
{
Next = curr->next;
curr->next = prev;
prev = curr;
curr = Next;
}
return prev;
}

Node *Sum(Node *head1, Node *head2)
{
head1 = reverse(head1);
head2 = reverse(head2);
Node *ans = new Node(0);
Node *temp;
temp = ans;
int carry = 0;
while (head1 != NULL && head2 != NULL)
{
int sum = head1->data + head2->data + carry;
int digit = sum % 10;
temp->next = new Node(digit);
temp = temp->next;
carry = sum / 10;
head1 = head1->next;
head2 = head2->next;
}

while (head1 != NULL)
{
int sum = head1->data + carry;
int digit = sum % 10;
temp->next = new Node(digit);
temp = temp->next;
carry = sum / 10;
head1 = head1->next;
}
while (head2 != NULL)
{
int sum = head2->data + carry;
int digit = sum % 10;
temp->next = new Node(digit);
temp = temp->next;
carry = sum / 10;
head2 = head2->next;
}
while (carry != 0)
{
int sum = carry;
int digit = sum % 10;
temp->next = new Node(digit);
temp = temp->next;

carry = sum / 10;
}
return reverse(ans->next);
}

int main()
{

Node *node1 = new Node(9);
Node *head = node1;

Node *tail = node1;
insertAtTail(tail, 9);

insertAtTail(tail, 3);
insertAtTail(tail, 4);
insertAtTail(tail, 5);
insertAtTail(tail, 6);
cout << "The first Linked List" << endl;
display(head);

cout << endl;

Node *node2 = new Node(5);
Node *head1 = node2;
Node *tail1 = node2;
insertAtTail(tail1, 6);
insertAtTail(tail1, 4);
insertAtTail(tail1, 8);
cout << "The second Linked List" << endl;
display(head1);

Node *temp = Sum(head, head1);
cout << "The sum of the given two linked list" << endl;

display(temp);

return 0;
}