Compare two Linked List of Strings
Last Updated :
15 Mar, 2023
Given two linked lists L1 and L2 in which in every node a string is stored. The task is to check whether the strings combining all the nodes are similar or not.
Examples:
Input: L1 = [“He”, “llo”, “wor”, “ld”],
L2 = [“H”, “e”, “ll”, “owo”, “r”, “ld”]
Output: true
Explanation: both lists makes the string of “Helloworld”.
Input: L1 = [“w”, “o”, “l”, “d”],
L2 = [“wo”, “d”, “rl”]
Output: false
Explanation: L1 makes “world” but L2 makes “wodrl” both are different.
Input: L1 = [“w”, “”, “orl”, “d”],
L2 = [“worl”, “”, “”, “”, “d”]
Output: true
Explanation: both lists makes the string of “world”.
Naive Approach: This is the simple approach. Traverse both the lists and store their values in another string, then compare both strings if equal return true else return false.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
string s;
Node* next;
Node(string s)
{
this ->s = s;
next = NULL;
}
};
bool compare(Node* list1, Node* list2)
{
string s1, s2;
while (list1 != NULL) {
s1 += list1->s;
list1 = list1->next;
}
while (list2 != NULL) {
s2 += list2->s;
list2 = list2->next;
}
return s1 == s2;
}
int main()
{
Node* n1 = new Node( "w" );
Node* head1 = n1;
Node* n2 = new Node( "" );
Node* n3 = new Node( "orl" );
Node* n4 = new Node( "d" );
Node* n5 = new Node( "worl" );
Node* head2 = n5;
Node* n6 = new Node( "" );
Node* n7 = new Node( "" );
Node* n8 = new Node( "nd" );
n1->next = n2;
n2->next = n3;
n3->next = n4;
n5->next = n6;
n6->next = n7;
n7->next = n8;
if (compare(head1, head2) == true )
cout << "true" ;
else
cout << "false" ;
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static class Node {
public String s;
public Node next;
public Node(String s) {
this .s = s;
next = null ;
}
};
public static boolean compare(Node list1, Node list2) {
String s1 = "" , s2 = "" ;
while (list1 != null ) {
s1 += list1.s;
list1 = list1.next;
}
while (list2 != null ) {
s2 += list2.s;
list2 = list2.next;
}
return s1.equals(s2);
}
public static void main(String[] args) {
Node n1 = new Node( "w" );
Node head1 = n1;
Node n2 = new Node( "" );
Node n3 = new Node( "orl" );
Node n4 = new Node( "d" );
Node n5 = new Node( "worl" );
Node head2 = n5;
Node n6 = new Node( "" );
Node n7 = new Node( "" );
Node n8 = new Node( "nd" );
n1.next = n2;
n2.next = n3;
n3.next = n4;
n5.next = n6;
n6.next = n7;
n7.next = n8;
if (compare(head1, head2) == true )
System.out.println( "true" );
else
System.out.println( "false" );
}
}
|
Python3
class Node:
def __init__( self , str ):
self .s = str
self . next = None
def compare(list1, list2):
s1,s2 = " "," "
while (list1 ! = None ):
s1 + = list1.s
list1 = list1. next
while (list2 ! = None ):
s2 + = list2.s
list2 = list2. next
return s1 = = s2
n1 = Node( "w" )
head1 = n1
n2 = Node("")
n3 = Node( "orl" )
n4 = Node( "d" )
n5 = Node( "worl" )
head2 = n5
n6 = Node("")
n7 = Node("")
n8 = Node( "nd" )
n1. next = n2
n2. next = n3
n3. next = n4
n5. next = n6
n6. next = n7
n7. next = n8
if (compare(head1, head2) = = True ):
print ( "true" )
else :
print ( "false" )
|
C#
using System;
using System.Collections.Generic;
class GFG{
public class Node {
public String s;
public Node next;
public Node(String s)
{
this .s = s;
next = null ;
}
};
static bool compare(Node list1, Node list2)
{
String s1 = "" , s2= "" ;
while (list1 != null ) {
s1 += list1.s;
list1 = list1.next;
}
while (list2 != null ) {
s2 += list2.s;
list2 = list2.next;
}
return s1 == s2;
}
public static void Main()
{
Node n1 = new Node( "w" );
Node head1 = n1;
Node n2 = new Node( "" );
Node n3 = new Node( "orl" );
Node n4 = new Node( "d" );
Node n5 = new Node( "worl" );
Node head2 = n5;
Node n6 = new Node( "" );
Node n7 = new Node( "" );
Node n8 = new Node( "nd" );
n1.next = n2;
n2.next = n3;
n3.next = n4;
n5.next = n6;
n6.next = n7;
n7.next = n8;
if (compare(head1, head2) == true )
Console.Write( "true" );
else
Console.Write( "false" );
}
}
|
Javascript
<script>
class Node {
constructor(str) {
this .s = str;
this .next = null ;
}
};
function compare(list1, list2)
{
let s1 = "" , s2 = "" ;
while (list1 != null ) {
s1 += list1.s;
list1 = list1.next;
}
while (list2 != null ) {
s2 += list2.s;
list2 = list2.next;
}
return s1 == s2;
}
let n1 = new Node( "w" );
let head1 = n1;
let n2 = new Node( "" );
let n3 = new Node( "orl" );
let n4 = new Node( "d" );
let n5 = new Node( "worl" );
let head2 = n5;
let n6 = new Node( "" );
let n7 = new Node( "" );
let n8 = new Node( "nd" );
n1.next = n2;
n2.next = n3;
n3.next = n4;
n5.next = n6;
n6.next = n7;
n7.next = n8;
if (compare(head1, head2) == true )
document.write( "true" );
else
document.write( "false" );
</script>
|
Time Complexity: O(N+M) where N and M are lengths of the strings.
Auxiliary Space: O(N+M)
Efficient Approach: This problem can be solved by using Two pointers approach. Follow the steps below to solve the given problem.
- Traverse both the lists while maintaining two pointers for the characters.
- Compare each of the characters separately. If different, return false otherwise, continue to compare.
- If the pointer’s value becomes the size of a string in a node, then move to the next node.
- Repeat the steps until the nodes do not become NULL.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
string s;
Node* next;
Node(string s)
{
this ->s = s;
next = NULL;
}
};
bool compare(Node* list1, Node* list2)
{
int i = 0, j = 0;
while (list1 != NULL && list2 != NULL) {
while (i < list1->s.size() &&
j < list2->s.size()) {
if (list1->s[i] != list2->s[j])
return false ;
i++;
j++;
}
if (i == list1->s.size()) {
i = 0;
list1 = list1->next;
}
if (j == list2->s.size()) {
j = 0;
list2 = list2->next;
}
}
return list1 == NULL && list2 == NULL;
}
int main()
{
Node* n1 = new Node( "w" );
Node* head1 = n1;
Node* n2 = new Node( "" );
Node* n3 = new Node( "orl" );
Node* n4 = new Node( "d" );
Node* n5 = new Node( "worl" );
Node* head2 = n5;
Node* n6 = new Node( "" );
Node* n7 = new Node( "" );
Node* n8 = new Node( "nd" );
n1->next = n2;
n2->next = n3;
n3->next = n4;
n5->next = n6;
n6->next = n7;
n7->next = n8;
if (compare(head1, head2) == true )
cout << "true" ;
else
cout << "false" ;
return 0;
}
|
Java
public class Compare {
static class Node {
String s;
Node next;
Node(String s)
{
this .s = s;
next = null ;
}
}
static boolean compare(Node list1, Node list2)
{
int i = 0 , j = 0 ;
while (list1 != null && list2 != null ) {
while (i < list1.s.length()
&& j < list2.s.length()) {
if (list1.s.charAt(i) != list2.s.charAt(j))
return false ;
i++;
j++;
}
if (i == list1.s.length()) {
i = 0 ;
list1 = list1.next;
}
if (j == list2.s.length()) {
j = 0 ;
list2 = list2.next;
}
}
return list1 == null && list2 == null ;
}
public static void main(String[] args)
{
Node n1 = new Node( "w" );
Node head1 = n1;
Node n2 = new Node( "" );
Node n3 = new Node( "orl" );
Node n4 = new Node( "d" );
Node n5 = new Node( "worl" );
Node head2 = n5;
Node n6 = new Node( "" );
Node n7 = new Node( "" );
Node n8 = new Node( "nd" );
n1.next = n2;
n2.next = n3;
n3.next = n4;
n5.next = n6;
n6.next = n7;
n7.next = n8;
if (compare(head1, head2) == true )
System.out.println( "true" );
else
System.out.println( "false" );
}
}
|
Python3
class Node:
def __init__( self ,s):
self .s = s
self . next = None
def compare(list1, list2):
i,j = 0 , 0
while (list1 ! = None and list2 ! = None ):
while (i < len (list1.s) and
j < len (list2.s)):
if (list1.s[i] ! = list2.s[j]):
return False
i + = 1
j + = 1
if (i = = len (list1.s)):
i = 0
list1 = list1. next
if (j = = len (list2.s)):
j = 0
list2 = list2. next
return list1 = = None and list2 = = None
n1 = Node( "w" )
head1 = n1
n2 = Node("")
n3 = Node( "orl" )
n4 = Node( "d" )
n5 = Node( "worl" )
head2 = n5
n6 = Node("")
n7 = Node("")
n8 = Node( "nd" )
n1. next = n2
n2. next = n3
n3. next = n4
n5. next = n6
n6. next = n7
n7. next = n8
if (compare(head1, head2) = = True ):
print ( "true" )
else :
print ( "false" )
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
static bool compare(Node list1, Node list2)
{
int i = 0, j = 0;
while (list1 != null && list2 != null ) {
while (i < list1.s.Length && j < list2.s.Length) {
if (list1.s[i] != list2.s[j])
return false ;
i++;
j++;
}
if (i == list1.s.Length) {
i = 0;
list1 = list1.next;
}
if (j == list2.s.Length) {
j = 0;
list2 = list2.next;
}
}
return list1 == null && list2 == null ;
}
public static void Main( string [] args){
Node n1 = new Node( "w" );
Node head1 = n1;
Node n2 = new Node( "" );
Node n3 = new Node( "orl" );
Node n4 = new Node( "d" );
Node n5 = new Node( "worl" );
Node head2 = n5;
Node n6 = new Node( "" );
Node n7 = new Node( "" );
Node n8 = new Node( "nd" );
n1.next = n2;
n2.next = n3;
n3.next = n4;
n5.next = n6;
n6.next = n7;
n7.next = n8;
if (compare(head1, head2) == true )
Console.WriteLine( "true" );
else
Console.WriteLine( "false" );
}
}
public class Node{
public String s;
public Node next;
public Node(String s)
{
this .s = s;
next = null ;
}
}
|
Javascript
<script>
class Node {
constructor(s)
{
this .s = s;
this .next = null ;
}
}
function compare(list1, list2)
{
let i = 0, j = 0;
while (list1 != null && list2 != null ) {
while (i < list1.s.length &&
j < list2.s.length) {
if (list1.s[i] != list2.s[j])
return false ;
i++;
j++;
}
if (i == list1.s.length) {
i = 0;
list1 = list1.next;
}
if (j == list2.s.length) {
j = 0;
list2 = list2.next;
}
}
return list1 == null && list2 == null ;
}
let n1 = new Node( "w" );
let head1 = n1;
let n2 = new Node( "" );
let n3 = new Node( "orl" );
let n4 = new Node( "d" );
let n5 = new Node( "worl" );
let head2 = n5;
let n6 = new Node( "" );
let n7 = new Node( "" );
let n8 = new Node( "nd" );
n1.next = n2;
n2.next = n3;
n3.next = n4;
n5.next = n6;
n6.next = n7;
n7.next = n8;
if (compare(head1, head2) == true )
document.write( "true" );
else
document.write( "false" );
</script>
|
Time Complexity: O(N+M) where N and M are length of the strings.
Auxiliary Space: O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...