Given two linked lists, check whether they represent the same number. Each node of the list represents part of the whole integer which needs to be matched. Return True if the lists match, otherwise return False.
The first line of the input contains an integer 'T' denoting the number of test cases.
The next '2T' line contains some integers each line denotes one linked list.
Elements of the linked list are separated by space. A linked list is terminated by -1.
Output Format:
For each test case, print True if the given lists match, else print False.
Note:
You do not need to print anything, it has already been taken care of. Just implement the function and return the answer.
1 <= T <= 10
1 <= N1 <= 10^4
1 <= N2 <= 10^4
Where N1, N2 represents the length of linked list one and two respectively.
Time Limit: 1 sec
2
0 0 1 2 -1
0 1 2 -1
43 5 678 -1
4 3 56 87 -1
True
False
Here we have 2 test cases, hence there are 2 pairs, a total of 4 linked lists. The second line and third line depict the first pair and the fourth line and the fifth line depicts the second pair of the linked list.
Test Case 1: The first linked list of the first pair represents the number 0012 which is 12 and the second linked list of the first pair represents the number 012 which is 12. Because both the linked list represents the same number hence the answer is true.
Test case 2: The first linked list of the second pair represents the number 435678 and the second linked list of the second pair represents the number 435687. Because both the linked list represents the different numbers hence the answer is false.
2
1 2 4 0 -1
12 4 2 -1
6 0 2 -1
2 4 1 -1
False
False
Try recursively converting both the linked list elements to something whole such as string which you can compare.
O(max(N1, N2)), Where N1 denotes the length of linked list-1 and N2 denotes the length of linked list-2.
As we are iterating two linked lists of size N1 and N2, time complexity will be O(max(N1, N2)).
O(max(N1, N2)) N1 denotes the length of linked list-1 and N2 denotes the length of linked list-2.
As we are using two strings of length N1 and N2. and due to recursion, we will require extra stack space of O(max(N1, N2)).