Compare two linked lists Hackerrank problem solution

Compare two linked lists Hackerrank

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.

Visit Link:-

https://coderinme.com/compare-two-link…problem-solution/ ‎

Input Format
You have to complete the int CompareLists(Node* headA, Node* headB) method which takes two arguments – the heads of the two linked lists to compare. You should NOT read any input from stdin/console.

Output Format
Compare the two linked lists and return 1 if the lists are equal. Otherwise, return 0. Do NOT print anything to stdout/console.

Sample Input

NULL, 1 –> NULL
1 –> 2 –> NULL, 1 –> 2 –> NULL

Sample Output

0
1
Explanation
1. We compare an empty list with a list containing 1. They don’t match, hence return 0.
2. We have 2 similar lists. Hence return 1.

Solution
/*
  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)
{
   if (headA == NULL && headB == NULL) {   
    return 1;
} else if (headA == NULL || headB == NULL) {
    return 0;
}
return (headA->data == headB->data) && CompareLists(headA->next, headB->next);
  
  // This is a "method-only" submission. 
  // You only need to complete this method 
}
/*
  Insert Node at the end of a linked list 
  head pointer input could be NULL as well for empty list
  Node is defined as 
  class Node {
     int data;
     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.data != headB.data)
                    return 0;

                headA = headA.next;
                headB = headB.next;

            }
    
    
    if(headA==null && headB!=null)
        return 0;
    
    if(headA!=null && headB==null)
        return 0;
            
            return 1;
  
}

#Body
"""
 Compare two linked list
 head could be None as well for empty list
 Node is defined as
 
 class Node(object):
 
   def __init__(self, data=None, next_node=None):
       self.data = data
       self.next = next_node

 return back the head of the linked list in the below method.
"""

def CompareLists(headA, headB):
    while headA != None and headB != None:
        if headA.data != headB.data:
            return 0
        headA = headA.next
        headB = headB.next
    if headA != None or headB != None:
        return 0
    return 1
  
  

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.