title: Check if two linked list are equal in java
body: <p>I am basically completing a hacker rank excercise where you return 1 if the list is completely equal meaning there are same number of nodes in two lists and all the values in nodes also equal. Otherwise you return 0.</p>
  
  <p>This is the method I wrote and for some reason I keep failing the test case i am not sure why. I wrote down some test cases in my book and did hand tracing and still can't seem to figure it out why.</p>
  
  <pre><code>int CompareLists(Node headA, Node headB) {
      // This is a "method-only" submission. 
      // You only need to complete this method 
      Node temp = headA;
      Node temp2 = headB;
      int equal = 1;
  
      if(temp == null &amp;&amp; temp2 == null){
          equal = 1;
      }
      else if((temp == null &amp;&amp; temp2 != null) || (temp!=null &amp;&amp; temp2 == null)){
          equal = 0;
      }
      else{
      while(temp.next != null){
          if(temp.data != temp2.data || temp2.next == null){
              equal = 0;
              break;
          }
          temp = temp.next;
          temp2 = temp2.next;
      }
      if(temp2.next != null){
          equal = 0;
      }
      }
  
    return equal;
  }
  </code></pre>
  
  <p>Yes i found many solutions online but I am more curious as to why my solution is not working.</p>