Compare two linked lists| Linked List
TASK (easy)
You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. If all data attributes are equal and the lists are the same length, return 1. Otherwise, return 0.
Example
llist1=1->2->3->NULL
llist2=1->2->3->4->NULL
The two lists have equal data attributes for the first nodes. is longer, though, so the lists are not equal. Return .
Function Description
Complete the compare_lists function in the editor below.
compare_lists has the following parameters:
- SinglyLinkedListNode llist1: a reference to the head of a list
- SinglyLinkedListNode llist2: a reference to the head of a list
Returns
- int: return 1 if the lists are equal, or 0 otherwise
Input Format
The first line contains an integer m, the number of test cases. Each of the test cases has the following format:
The first line contains an integer m, the number of nodes in the first linked list.
Each of the next m lines contains an integer, each a value for a data attribute.
The next line contains an integer m, the number of nodes in the second linked list.
Each of the next m lines contains an integer, each a value for a data attribute.
Constraints
1<=t<=10
1<=n, m<=1000
1<=llist1[i],llist2[i]<=1000
Output Format
Compare the two linked lists and return 1 if the lists are equal. Otherwise, return 0. Do NOT print anything to stdout/console.
The output is handled by the code in the editor and it is as follows:
For each test case, in a new line, print 1 if the two lists are equal, else print 0.
Sample Input
2
2
1
2
1
1
2
1
2
2
1
2
Sample Output
0
1
SOLUTION 1
def compare_lists(llist1, llist2):
temp_ptr1 = llist1
temp_ptr2 = llist2
while temp_ptr1 !=None and temp_ptr2 !=None:
if temp_ptr1.data ==temp_ptr2.data:
temp_ptr1 = temp_ptr1.next
temp_ptr2 = temp_ptr2.next
else:
return 0
if temp_ptr1==None and temp_ptr2 ==None:
return 1
else:
return 0
EXPLAINATION STEPS
1.Initialize: ptr1 and ptr2 to the heads of the lists.
2.Traverse: Compare node values and move to the next node.
3.Check End: Ensure both lists end at the same time. If so, lists are identical.