Write an algorithm to check equality of two link lists. IN JAVA OR PSEUDOCODE
This is the pseudocode for the algorithm. We assume that each node of the linkedlist is of the form (data,next), where data is the value of that node and next contains the pointer to the next element of the linkedlist.
Our function returns false is the two linked lists are not equal else we return true.
We first check if the length of both the lists are 0, if they are 0 then we return true.
Then we iterate over each node to check if they are equal. Finally we check if both the linkedlist has the same end point. If they don't have the same endpoint, we return false.
function check_equality(list1, list2){
if( list1 == none and list2 == none):
return true
head1 = list1
head2 = list2
while( head1 != none and head2 != none):
if( head1.data != head2.data):
return false
else:
head1 = head1.next
head2 = head2.next
if( head1 != none or head2 != none):
return false
return true
}
Get Answers For Free
Most questions answered within 1 hours.