The first line of input contains a single integer T, representing the number of test cases or queries to be run.
Then the T test cases follow.
The first line of each test case contains the elements of the first singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
The second line of each test case contains the elements of the second singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
For each test case, print the sum linked list. The elements of the sum list should be single-space separated, terminated by -1.
The output of each test case is printed in a separate line.
You do not need to print anything, just return the head of the sum linked list.
1 <= T <= 10
1 <= N <= 5 * 10^4
0 <= data <= 9 and data != -1
Time Limit : 1sec
One way is to recursively add the two linked lists. Keep the nodes in the recursion stack and add the last nodes first and then second last and so on. Initially, find the size of both the linked lists. If both the linked lists are of the same size, add them using recursion. Else if their sizes differ, move the head pointer of the larger linked list forward K times, where K is the difference between the number of nodes in the larger linked list and the smaller one. Now calculate the sum of these two linked lists using recursion and add the carry of the resultant list and the left part of the larger linked list.
Algorithm :
The idea is to use the simple method of adding two numbers. We add two numbers starting from their least significant digit and moving towards the most significant digit. So we need to access the nodes of linked lists from last to first. First step would be to reverse both the linked lists. Then add both the linked lists starting from their heads and then reverse the resulting sum list and return its head.
Algorithm :
Deletion In Doubly Linked List
Deletion In Doubly Linked List
Deletion In Doubly Linked List
Insertion In Doubly Linked List
Insertion In Doubly Linked List
Insertion In Doubly Linked List
Insertion In Doubly Linked List
LRU Cache
Delete Nodes On Regular Intervals
Add One To Linked List