Compare two linked lists | Practice | GeeksforGeeks

Given two string, represented as linked lists (every character is a node->data in the linked list). Write a function compare() that works similar to strcmp(), i.e., it returns 0 if both strings are same, 1 if first linked list is lexicographically greater, and -1 if second is lexicographically greater.
Input:
First line of input contains number of testcases T. For each testcase, there will be 4 lines of input. First line of which contains length of first linked list and next line contains the linked list, similarly next two lines contains length and linked list respectively.
Output:
Comapare two strings represented as linked list.
User Task:
The task is to complete the function compare() which compares the strings through linked list and returns 0, 1 or -1 accordingly.
Constraints:
1 <= T <= 100
1 <= N, M <= 100
Example:
Input:
2
5
a b a b a
4
a b a a
3
a a b
3
a a b
Output:
1
0
Explanation:
Testcase 1: String consisting of nodes of first linked list is lexicographically greater than the second one. So, the result is 1.
 
0
tthakare734 days ago
//java solution -> TC -> 0.29

class GfG{
    int compare(Node node1, Node node2){
        
        while(node1 != null && node2 != null){
            if(node1.data > node2.data) return 1;
            if(node2.data > node1.data) return -1;
            node1 = node1.next;
            node2 = node2.next;
        }

        return 0;
    }
}
+1
shahabuddinbravo406 days ago
int compare(Node *list1, Node *list2) { 
//***********method1**************************   //  Node *ptr1=list1,*ptr2=list2;   //  string s1="",s2="";       //  while(ptr1!=NULL){   //     s1.push_back(ptr1->c);   //     ptr1=ptr1->next;   //  }       //  while(ptr2!=NULL){   //      s2.push_back(ptr2->c);   //      ptr2=ptr2->next;   //  }       //  if(s1==s2){   //      return 0;   //  }   //  else if(s1>s2){   //      return 1;   //  }   //  else{   //      return -1;   //  }   
// ***************method2****************;   Node *ptr1=list1,*ptr2=list2;      while(ptr1!=NULL && ptr2!=NULL){       if(ptr1->c > ptr2->c){           return 1;       }       else if(ptr1->c < ptr2->c){           return -1;       }              ptr1=ptr1->next;       ptr2=ptr2->next;   }      if(ptr1==NULL && ptr2==NULL){       return 0;   }      if(ptr2==NULL){       return 1;   }      if(ptr1==NULL){       return -1;   }   }
+1
shahabuddinbravo406 days ago
int compare(Node *list1, Node *list2) {    Node *ptr1=list1,*ptr2=list2;    string s1="",s2="";        while(ptr1!=NULL){       s1.push_back(ptr1->c);       ptr1=ptr1->next;    }        while(ptr2!=NULL){        s2.push_back(ptr2->c);        ptr2=ptr2->next;    }        if(s1==s2){        return 0;    }    else if(s1>s2){        return 1;    }    else{        return -1;    }   }
0
mallasarj102 weeks ago
C++ Solution : Comapre characters if not same otherwise return 0
bool comp (char s1, char s2){   return tolower(s1)<tolower(s2);}// Compare two strings represented as linked listsint compare(Node *list1, Node *list2) {    Node* cur1 = list1;    Node* cur2 = list2;        while(cur1 && cur2)    {        if(cur1->c != cur2->c)        {            if(comp(cur1->c, cur2->c))            return -1;            else            return 1;        }        cur1=cur1->next;        cur2=cur2->next;    }    return 0;}
0
ayoakore2 weeks ago
// JavaScript Solution
 
class Solution { compare(list1,list2){          let l1 = list1;     let l2 = list2;      while (l1 !== null && l2 !== null){       if (l1.data > l2.data)       {           return 1;       }       if (l2.data > l1.data)        {           return -1;       }       l1 = l1.next;       l2 = l2.next;   }       if (l1 === null && l2 !== null)        {           return -1;       }       if (l2 === null && l1 !== null)        {           return 1;       }       return 0; }}
+1
gupta2411sumit1 month ago
// Easy C++ Solution 
 int compare(Node node1, Node node2)   {     //Your code here          Node p = node1 ;     Node q = node2 ;          boolean flag = true ;     while( p!=null && q!=null)     {         if( p.data > q.data )         {             return 1 ;         }                  if( q.data > p.data )         {             return -1 ;         }                  p = p.next ;         q = q.next ;     }          if(p==null && q!=null)     {         return -1 ;     }          if(p!=null && q==null)     {         return 1 ;     }         return 0 ;   }
0
alam214002 months ago
int compare(Node *list1, Node *list2) {    // Code Here    int f=0;    while(list1!=NULL && list2!=NULL)    {                if(list1->c!=list2->c)        {f=1;           break;        }        list1=list1->next;        list2=list2->next;    }    if(f==0)    return 0;    if(list2==NULL||list1->c>list2->c)    return 1;    if(list1==NULL ||list1->c<list2->c)     return -1;     }
+1
chetanum212 months ago
                               simple c code
int compare(Node *list1, Node *list2) {    // Code Here    Node *cur1=list1;    Node *cur2=list2;    while(cur1!=NULL && cur2!=NULL)    {        if(cur1->c==cur2->c)        {            cur1=cur1->next;            cur2=cur2->next;        }        else        {            if((int)(cur1->c)>(int)(cur2->c))                return 1;            else               return -1;        }    }    return 0;} 
0
emmanueluluabuike2 months ago
Simple Java Solution
int compare(Node node1, Node node2) {
      //Your code here
      Node n1 = node1;
      Node n2 = node2;
      
      while(n1 != null && n2 != null){
          if(n1.data > n2.data)
            return 1;
        else if(n2.data > n1.data)
            return -1;
          n1 = n1.next;
          n2 = n2.next;
      }
      
      return 0;
    }
0
yashkotalwar102 months ago
// JAVA
Node first = node1;       Node second = node2;       int same = 0;       while (first != null && second != null) {           if (first.data == second.data){               first = first.next;               second = second.next;           }           else if (first.data > second.data){               return 1;           }           else {               return -1;           }       }       return 0;
 
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.