📜  检查两个链表是否是字谜

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

检查两个链表是否是字谜

给定两个链表形式的字符串,任务是检查一个字符串是否是另一个字符串的变位。如果是则打印Yes ,否则打印No

例子:

方法:这个问题可以通过对链表进行排序然后检查它们是否相同来解决。如果它们在排序后相同,则它们是字谜。否则,他们不是。现在按照以下步骤解决此问题:

  1. 使用冒泡排序对两个链表进行排序。
  2. 现在,从头开始遍历两个链表并匹配每个相应的节点。
  3. 如果两个对应的节点不同,则打印No并返回。
  4. 循环结束后最后打印Yes

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
  
// Structure for a node
struct Node {
    int data;
    Node* next;
} Node;
  
// Function to swap the nodes
struct Node* swap(struct Node* ptr1, 
                  struct Node* ptr2)
{
    struct Node* tmp = ptr2->next;
    ptr2->next = ptr1;
    ptr1->next = tmp;
    return ptr2;
}
  
// Function to sort the list
void bubbleSort(struct Node*** head, 
                int count)
{
    struct Node** h;
    int i, j, swapped;
  
    for (i = 0; i <= count; i++) {
  
        h = *head;
        swapped = 0;
  
        for (j = 0; j < count - i - 1; 
             j++) {
  
            struct Node* p1 = *h;
            struct Node* p2 = p1->next;
  
            if (p1->data > p2->data) {
  
                // Update the link 
                // after swapping
                *h = swap(p1, p2);
                swapped = 1;
            }
  
            h = &(*h)->next;
        }
  
        // Break if the loop ended 
        // without any swap
        if (swapped == 0)
            break;
    }
}
  
// Function to insert a struct Node
// at the end of a linked list
void insert(struct Node*** head, int data)
{
    struct Node* ptr = new struct Node();
  
    ptr->data = data;
    ptr->next = NULL;
    if (**head == NULL) {
        **head = ptr;
    }
    else {
        struct Node* ptr1 = **head;
        while (ptr1->next != NULL) {
            ptr1 = ptr1->next;
        }
        ptr1->next = ptr;
    }
}
  
// Function to check if both strings 
// are anagrams or not
bool checkAnagram(struct Node** head1, 
                  struct Node** head2,
                  int N, int M)
{
  
    // Sort the linked list
    bubbleSort(&head1, N);
    bubbleSort(&head2, M);
  
    struct Node* ptr1 = *head1;
    struct Node* ptr2 = *head2;
    int flag = 0;
    while (ptr1 != NULL) {
        if (ptr1->data == ptr2->data) {
            ptr1 = ptr1->next;
            ptr2 = ptr2->next;
        }
        else {
            return 0;
        }
    }
  
    // If one of the linked list
    // doesn't end
    if (!ptr1 and !ptr2) {
        return 1;
    }
  
    return 0;
}
  
// Function to create linked lists
// from the strings
void createLinkedList(struct Node** head1,
                      struct Node** head2, 
                      string s1, string s2)
{
  
    int N = s1.size();
    int M = s2.size();
  
    // Create linked list from the array s1
    for (int i = 0; i < N; i++) {
        insert(&head1, s1[i]);
    }
  
    // Create linked list from the array s2
    for (int i = 0; i < M; i++) {
        insert(&head2, s2[i]);
    }
}
  
// Driver Code
int main()
{
    string s1 = "TRIANGLE";
    string s2 = "INTEGRAL";
  
    int N = s1.size(), M = s2.size();
  
    // start with empty linked list
    struct Node* head1 = NULL;
    struct Node* head2 = NULL;
  
    createLinkedList(&head1, &head2, s1, s2);
  
    if (checkAnagram(&head1, &head2, N, M))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


输出
Yes

时间复杂度: O(N 2 )
辅助空间: O(1)