Update Add-Two-linkedlist.cpp

pull/1019/head
Abhishek Anand 2022-10-16 07:03:30 +05:30 committed by GitHub
parent 6de82be85a
commit f75757265d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 31 deletions

View File

@ -1,6 +1,6 @@
/* In this algorithm two linked list is given in which we have to add both the linked list and store the output in another linked list. /* In this algorithm two linked list is given in which we have to add both the linked list and store the output in another linked list.
Time Complexity: O(m+n) where m and n are the sizes of given two linked lists. Time Complexity: O(m+n) where m and n are the sizes of given two linked lists.
Space Complexity: O(m+n) where m and n are the size of the given two linked list.
Space Complexity: O(m) if (m>n) and O(n) if (n>m) ,where m and n are the size of the given two linked list.*/ Space Complexity: O(m) if (m>n) and O(n) if (n>m) ,where m and n are the size of the given two linked list.*/
@ -12,13 +12,11 @@ class Node
public: public:
int data; int data;
Node *next; Node *next;
Node(int val) Node(int val)
{ {
this->data = val; this->data = val;
this->next = NULL; this->next = NULL;
} }
~Node() ~Node()
{ {
int value = this->data; int value = this->data;
@ -30,7 +28,6 @@ public:
cout << "Memory is free for the node with data" << value << endl; cout << "Memory is free for the node with data" << value << endl;
} }
}; };
void insertAtHead(Node *&head, int data) void insertAtHead(Node *&head, int data)
{ {
Node *temp = head; Node *temp = head;
@ -42,10 +39,9 @@ void insertAtHead(Node *&head, int data)
newNode->next = head; newNode->next = head;
head = newNode; head = newNode;
} }
void insertAtTail(Node *&head, int d) void insertAtTail(Node *&head, int d)
{ {
// new node create
if (head == NULL) if (head == NULL)
{ {
insertAtHead(head, d); insertAtHead(head, d);
@ -54,7 +50,6 @@ void insertAtTail(Node *&head, int d)
head->next = temp; head->next = temp;
head = temp; head = temp;
} }
void display(Node *&head) void display(Node *&head)
{ {
if (head == NULL) if (head == NULL)
@ -70,7 +65,6 @@ void display(Node *&head)
} }
cout << "NULL" << endl; cout << "NULL" << endl;
} }
Node *reverse(Node *head) Node *reverse(Node *head)
{ {
Node *curr = head; Node *curr = head;
@ -85,7 +79,6 @@ Node *reverse(Node *head)
} }
return prev; return prev;
} }
Node *Sum(Node *head1, Node *head2) Node *Sum(Node *head1, Node *head2)
{ {
head1 = reverse(head1); head1 = reverse(head1);
@ -104,7 +97,6 @@ Node *Sum(Node *head1, Node *head2)
head1 = head1->next; head1 = head1->next;
head2 = head2->next; head2 = head2->next;
} }
while (head1 != NULL) while (head1 != NULL)
{ {
int sum = head1->data + carry; int sum = head1->data + carry;
@ -129,30 +121,23 @@ Node *Sum(Node *head1, Node *head2)
int digit = sum % 10; int digit = sum % 10;
temp->next = new Node(digit); temp->next = new Node(digit);
temp = temp->next; temp = temp->next;
carry = sum / 10; carry = sum / 10;
} }
return reverse(ans->next); return reverse(ans->next);
} }
int main() int main()
{ {
Node *node1 = new Node(9); Node *node1 = new Node(9);
Node *head = node1; Node *head = node1;
Node *tail = node1; Node *tail = node1;
insertAtTail(tail, 9); insertAtTail(tail, 9);
insertAtTail(tail, 3); insertAtTail(tail, 3);
insertAtTail(tail, 4); insertAtTail(tail, 4);
insertAtTail(tail, 5); insertAtTail(tail, 5);
insertAtTail(tail, 6); insertAtTail(tail, 6);
cout << "The first Linked List" << endl; cout << "The first Linked List" << endl;
display(head); display(head);
cout << endl; cout << endl;
Node *node2 = new Node(5); Node *node2 = new Node(5);
Node *head1 = node2; Node *head1 = node2;
Node *tail1 = node2; Node *tail1 = node2;
@ -161,12 +146,9 @@ int main()
insertAtTail(tail1, 8); insertAtTail(tail1, 8);
cout << "The second Linked List" << endl; cout << "The second Linked List" << endl;
display(head1); display(head1);
Node *temp = Sum(head, head1); Node *temp = Sum(head, head1);
cout << "The sum of the given two linked list" << endl; cout << "The sum of the given two linked list" << endl;
display(temp); display(temp);
return 0; return 0;
} }
@ -178,11 +160,3 @@ The second Linked List
5-> 6-> 4-> 8-> NULL 5-> 6-> 4-> 8-> NULL
The sum of the given two linked list The sum of the given two linked list
9-> 9-> 9-> 1-> 0-> 4-> NULL */ 9-> 9-> 9-> 1-> 0-> 4-> NULL */