Compare two linked lists hackerrank solution in Java

Compare two linked lists

In 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

  1. They have the same number of nodes
  2. Corresponding nodes contain the same Data.

For Example:

Input

2 // size of First List 1 2 1 // Size of Second List 1

We have two lists:

first : 1->2->NULL Second: 1->NULL

We can see that two lists are not equal so we will return 0 as our Answer.

Output 0

Here, 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 Caring

  • WhatsApp
  • Facebook
  • Twitter

Like this:

Like Loading...

Related