Saturday, 18 April 2015

Hackerrank Compare two linked lists Solution

Problem Statement

This challenge is part of a tutorial track by MyCodeSchool

You’re given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty.

source code:

  Compare two linked lists A and B  
  Return 1 if they are identical and 0 if they are not.   
  Node is defined as   
  struct Node  
    int data;  
    struct Node *next;  
 int CompareLists(Node *headA, Node* headB)  
  // This is a "method-only" submission.   
  // You only need to complete this method   
   while((headA!= NULL) || (headB!= NULL)){  
     if((headA == NULL) || (headB == NULL))  
       return 0;  
     if(headA->data != headB->data){  
       return 0;  
     headA = headA->next;  
     headB = headB->next;  
   return 1;  

** The above solution is my own code and it may not be the optimal solution or optimal way to approach the problem but it passes all the testcases in Hackerrank. So if you have any optimal approaches feel free to paste the code as the comment below..... :) :) :)


  1. It doesn't work.It says "‘null’ was not declared in this scope".

  2. Hackerrank compare two linked lists solution. First get old list and after that get the other one and it become more easy for the best essay to create difference and you also learn something more from which help these lists which give your different list.