Compare two strings represented as linked lists
Given two strings, represented as linked lists (every character is a node in a linked list). Write a function compare() that works similar to strcmp(), i.e., it returns 0 if both strings are the same, 1 if the first linked list is lexicographically greater, and -1 if the second string is lexicographically greater.
Examples:
Input: list1 = g->e->e->k->s->a
list2 = g->e->e->k->s->b
Output: -1
Explanation: “geeksb” is lexicographically greater than “geeksa”.
Input: list1 = g->e->e->k->s->a
list2 = g->e->e->k->s
Output: 1
Explanation: “geeksa” is greater than “geeks”.
Input: list1 = g->e->e->k->s
list2 = g->e->e->k->s
Output: 0
Explanation: Both the strings are “geeks”.
Naive Approach: Traverse the linked list and form the strings. Now compare the strings to check if both the strings are same or not.
Time Complexity: O(N + M) where N and M are the lengths of the linked lists.
Auxiliary Space: O(N + M)
Efficient Approach: An efficient approach is to use two pointer algorithm. Follow the steps mentioned below:
- Create two pointers and point to the starting of the linked lists.
- Traverse both the lists. If the characters are same keep traversing and increment both the pointers.
- If there is a mismatch:
- If character at that node of list1 is lexicographically greater print 1 and stop iteration.
- If character at that node of list2 is lexicographically greater print -1 and stop iteration.
- If traversal reaches the end of one of the list:
- If the end of the other list is also reached both strings are same.
- Else the other one is the lexicographically bigger one.
Below is the implementation of the above approach.
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
char c;
struct Node *next;
};
Node* newNode( char c)
{
Node *temp = new Node;
temp->c = c;
temp->next = NULL;
return temp;
};
int compare(Node *list1, Node *list2)
{
while (list1 && list2 &&
list1->c == list2->c)
{
list1 = list1->next;
list2 = list2->next;
}
if (list1 && list2)
return (list1->c > list2->c)? 1: -1;
if (list1 && !list2) return 1;
if (list2 && !list1) return -1;
return 0;
}
int main()
{
Node *list1 = newNode( 'g' );
list1->next = newNode( 'e' );
list1->next->next = newNode( 'e' );
list1->next->next->next = newNode( 'k' );
list1->next->next->next->next =
newNode( 's' );
list1->next->next->next->next->next =
newNode( 'b' );
Node *list2 = newNode( 'g' );
list2->next = newNode( 'e' );
list2->next->next = newNode( 'e' );
list2->next->next->next = newNode( 'k' );
list2->next->next->next->next =
newNode( 's' );
list2->next->next->next->next->next =
newNode( 'a' );
cout << compare(list1, list2);
return 0;
}
|
Java
class LinkedList {
Node head;
static Node a, b;
static class Node {
char data;
Node next;
Node( char d) {
data = d;
next = null ;
}
}
int compare(Node node1, Node node2) {
if (node1 == null && node2 == null ) {
return 1 ;
}
while (node1 != null && node2 != null && node1.data == node2.data) {
node1 = node1.next;
node2 = node2.next;
}
if (node1 != null && node2 != null ) {
return (node1.data > node2.data ? 1 : - 1 );
}
if (node1 != null && node2 == null ) {
return 1 ;
}
if (node1 == null && node2 != null ) {
return - 1 ;
}
return 0 ;
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
Node result = null ;
list.a = new Node( 'g' );
list.a.next = new Node( 'e' );
list.a.next.next = new Node( 'e' );
list.a.next.next.next = new Node( 'k' );
list.a.next.next.next.next = new Node( 's' );
list.a.next.next.next.next.next = new Node( 'b' );
list.b = new Node( 'g' );
list.b.next = new Node( 'e' );
list.b.next.next = new Node( 'e' );
list.b.next.next.next = new Node( 'k' );
list.b.next.next.next.next = new Node( 's' );
list.b.next.next.next.next.next = new Node( 'a' );
int value;
value = list.compare(a, b);
System.out.println(value);
}
}
|
Python3
class Node:
def __init__( self , key):
self .c = key ;
self . next = None
def compare(list1, list2):
while (list1 and list2 and list1.c = = list2.c):
list1 = list1. next
list2 = list2. next
if (list1 and list2):
return 1 if list1.c > list2.c else - 1
if (list1 and not list2):
return 1
if (list2 and not list1):
return - 1
return 0
list1 = Node( 'g' )
list1. next = Node( 'e' )
list1. next . next = Node( 'e' )
list1. next . next . next = Node( 'k' )
list1. next . next . next . next = Node( 's' )
list1. next . next . next . next . next = Node( 'b' )
list2 = Node( 'g' )
list2. next = Node( 'e' )
list2. next . next = Node( 'e' )
list2. next . next . next = Node( 'k' )
list2. next . next . next . next = Node( 's' )
list2. next . next . next . next . next = Node( 'a' )
print (compare(list1, list2))
|
C#
using System;
public class LinkedList
{
public Node head;
public Node a, b;
public class Node
{
public char data;
public Node next;
public Node( char d)
{
data = d;
next = null ;
}
}
int compare(Node node1, Node node2)
{
if (node1 == null && node2 == null )
{
return 1;
}
while (node1 != null &&
node2 != null &&
node1.data == node2.data)
{
node1 = node1.next;
node2 = node2.next;
}
if (node1 != null && node2 != null )
{
return (node1.data > node2.data ? 1 : -1);
}
if (node1 != null && node2 == null )
{
return 1;
}
if (node1 == null && node2 != null )
{
return -1;
}
return 0;
}
public static void Main(String[] args)
{
LinkedList list = new LinkedList();
list.a = new Node( 'g' );
list.a.next = new Node( 'e' );
list.a.next.next = new Node( 'e' );
list.a.next.next.next = new Node( 'k' );
list.a.next.next.next.next = new Node( 's' );
list.a.next.next.next.next.next = new Node( 'b' );
list.b = new Node( 'g' );
list.b.next = new Node( 'e' );
list.b.next.next = new Node( 'e' );
list.b.next.next.next = new Node( 'k' );
list.b.next.next.next.next = new Node( 's' );
list.b.next.next.next.next.next = new Node( 'a' );
int value;
value = list.compare(list.a, list.b);
Console.WriteLine(value);
}
}
|
Javascript
<script>
var head;
var a, b;
class Node {
constructor(d) {
this .data = d;
this .next = null ;
}
}
function compare(node1, node2) {
if (node1 == null && node2 == null ) {
return 1;
}
while (node1 != null && node2 != null &&
node1.data == node2.data) {
node1 = node1.next;
node2 = node2.next;
}
if (node1 != null && node2 != null ) {
return (node1.data > node2.data ? 1 : -1);
}
if (node1 != null && node2 == null ) {
return 1;
}
if (node1 == null && node2 != null ) {
return -1;
}
return 0;
}
var result = null ;
a = new Node( 'g' );
a.next = new Node( 'e' );
a.next.next = new Node( 'e' );
a.next.next.next = new Node( 'k' );
a.next.next.next.next = new Node( 's' );
a.next.next.next.next.next = new Node( 'b' );
b = new Node( 'g' );
b.next = new Node( 'e' );
b.next.next = new Node( 'e' );
b.next.next.next = new Node( 'k' );
b.next.next.next.next = new Node( 's' );
b.next.next.next.next.next = new Node( 'a' );
var value;
value = compare(a, b);
document.write(value);
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(1)
Last Updated :
13 Jul, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...