Compare two linked lists
https://www.hackerrank.com/challenges/compare-two-linked-lists/problem
bool compare_lists(SinglyLinkedListNode* head1, SinglyLinkedListNode* head2) {
if(head1==NULL && head2!=NULL) return 0;
if(head2==NULL && head1!=NULL) return 0;
if(head1==NULL && head2==NULL) return 1;
SinglyLinkedListNode* temp1 =head1;
SinglyLinkedListNode* temp2 =head2;
while(temp1->next!=NULL && temp2->next!=NULL)
{
if(temp1->data==temp2->data)
{
temp1=temp1->next;
temp2=temp2->next;
}
else if(temp1->data!=temp2->data)
{
return 0;
}
}
if(temp1->next!=NULL || temp2->next!=NULL) return 0;
return 1;
}
Comments
Post a Comment