📜  比较两个字符串链表

📅  最后修改于: 2022-05-13 01:57:06.754000             🧑  作者: Mango

比较两个字符串链表

给定两个链表L1L2 ,其中每个节点都存储一个字符串。任务是检查组合所有节点的字符串是否相似。

例子:

天真的方法:这是简单的方法。遍历两个列表并将它们的值存储在另一个字符串中,然后比较两个字符串,如果相等则返回 true,否则返回 false。

下面是上述方法的实现。

C++
// C++ program for above approach
#include 
using namespace std;
 
// Structure of Node of linked list
class Node {
public:
    string s;
    Node* next;
    Node(string s)
    {
        this->s = s;
        next = NULL;
    }
};
 
// Function to compare if two linkedlists
// are similar or not
bool compare(Node* list1, Node* list2)
{
    // Declare string variables to store
    // the strings formed from the linked lists
    string s1, s2;
 
    while (list1 != NULL) {
        s1 += list1->s;
        list1 = list1->next;
    }
    while (list2 != NULL) {
        s2 += list2->s;
        list2 = list2->next;
    }
    return s1 == s2;
}
 
// Driver Code
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
// Java program for above approach
import java.util.*;
 
class GFG{
 
// Structure of Node of linked list
static class Node {
    String s;
    Node next;
    Node(String s)
    {
        this.s = s;
        next = null;
    }
};
 
// Function to compare if two linkedlists
// are similar or not
static boolean compare(Node list1, Node list2)
{
   
    // Declare String variables to store
    // the Strings formed from the linked lists
    String s1 ="", s2="";
 
    while (list1 != null) {
        s1 += list1.s;
        list1 = list1.next;
    }
    while (list2 != null) {
        s2 += list2.s;
        list2 = list2.next;
    }
    return s1 == s2;
}
 
// Driver Code
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.print("true");
    else
        System.out.print("false");
}
}
 
// This code is contributed by umadevi9616


Javascript


C++
// C++ program for above approach
#include 
using namespace std;
 
// Structure of Node of a linked list
class Node {
public:
    string s;
    Node* next;
    Node(string s)
    {
        this->s = s;
        next = NULL;
    }
};
 
// Compare function
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;
}
 
// Driver Code
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;
}



输出
false

时间复杂度: O(N+M) 其中 N 和 M 是字符串的长度。
辅助空间: O(N+M)

有效的方法:这个问题可以通过使用两个指针的方法来解决。请按照以下步骤解决给定的问题。

  • 遍历两个列表,同时保持两个字符指针
  • 分别比较每个字符。如果不同,则返回false ,否则继续比较。
  • 如果指针的值变成节点中字符串的大小,则移动到下一个节点。
  • 重复这些步骤,直到节点不变为NULL

下面是上述方法的实现。

C++

// C++ program for above approach
#include 
using namespace std;
 
// Structure of Node of a linked list
class Node {
public:
    string s;
    Node* next;
    Node(string s)
    {
        this->s = s;
        next = NULL;
    }
};
 
// Compare function
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;
}
 
// Driver Code
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;
}


输出
false

时间复杂度: O(N+M) 其中 N 和 M 是字符串的长度。
辅助空间:O(1)。