Compare two linked lists hackerrank solution in Java
Compare two linked listsIn this problem, we have to compare the data of the node if they are equal or not. Two Lists are equal if and only if
For Example: Input 2 // size of First List 1 2 1 // Size of Second List 1We have two lists: We can see that two lists are not equal so we will return 0 as our Answer. Output 0Here, I have represented the logic of the Compare two Linked Lists in C++. Please Dry and Run the code for the better Understanding. bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2)
{
while(head1->next!=NULL && head2->next!=NULL)
{
if(head1->data!=head2->data)
{
return false;
}
head1=head1->next;
head2=head2->next;
}
if(head1->next!=NULL || head2->next!=NULL)
{
return false;
}
else
{
return true;
}
} Reverse a Linked List : HackerRank Solution in C++ Delete a Node : HackerRank Solution in C++ Print in Reverse : HackerRank Solution in C++ Insert node at position : HackerRank Solution in C++ Insert Node at head : HackerRank Solution in C++ Insert node at tail : HackerRank Solution in C++ Cycle Detection: HackerRank Solution in C++ Hacker Rank Solutions: Find Merge Point of Two Lists Hacker Rank Solution: Merge two sorted linked lists Sharing is CaringLike this:Like Loading... Related |