Sakshi Singh
1 min readDec 31, 2018

HACKERRANK SOLUTION: Compare two linked lists

//COPY PASTE THIS PART OF CODE IN THE GIVEN BLANK SPACE OF YOUR EDITOR :)

static boolean compareLists(SinglyLinkedListNode head1, SinglyLinkedListNode head2) {

SinglyLinkedListNode n1 = head1;

SinglyLinkedListNode n2 = head2;

int count1=0,count2=0,count=0;

while(n1!=null)

{

count1++;

n1 = n1.next;

}

while(n2!=null)

{

count2++;

n2=n2.next;

}

if(count1 != count2)

return false;

else{

while(head1!=null || head2!=null)

{

if(head1.data == head2.data)

{

count++;

}

head1=head1.next;

head2=head2.next;

}

if(count == count1)

return true;

else

return false;

}

}

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Sakshi Singh
Sakshi Singh

Responses (1)

Write a response