Skip to content
Related Articles
Open in App
Not now

Related Articles

Check if two Circular Linked Lists are identical

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 30 Oct, 2022
Improve Article
Save Article

Given two circular linked lists L1 and L2, the task is to find if the two circular linked lists are identical or not. 

Note: Head of any linked list points to any node of the respective linked list and the lists can contain duplicate elements.

Examples:

Input: L1: 1 -> 2 -> 3 -> 4 -> 5 -> 1 -> 2 -> 6
           L2: 5 -> 1 -> 2 -> 6 -> 1 -> 2 -> 3 -> 4
Output: Identical
Explanation: If checked the 5th element of L1 and 1st element of L2 then they are identical.
As they are circular, does not matter from where we start checking.

Input: L1: 1 -> 2 -> 3
           L2: 1 ->3 -> 2
Output: Not Identical

 

Approach: The problem can be solved by traversing the circular linked list using the following idea:

This idea to solve this problem is similar to the problem “Check if given strings are rotations of each other or not“. We’ll convert given circular linked list into string and then check if second string is rotation of first string or not or vice versa.

Follow the steps below to implement the above idea:

  • Convert the first circular linked list into a string say S.
  • Convert the second circular linked list into a string say T.
  • Check lengths of S and T are not equal
    • If true, then return false.
  • Store the concatenation of S with S itself in variable temp (i.e, temp = S+S).
  • Check if T is a substring in temp.
  • If true, then return true.
  • Otherwise, return false.

Below is the implementation of the above approach:

C++




// C++ program to Check that two circular
// linked list are identical or not
 
#include <bits/stdc++.h>
using namespace std;
 
// Circular Linked list Node Class
class Node {
public:
    int data;
    Node* next;
 
    // Constructor function
    Node(int data)
    {
        this->data = data;
        this->next = NULL;
    }
};
 
// Function to insert a node in
// tail in circular linked list
void insertNode(Node*& head, Node*& tail, int d)
{
    // First insertion in circular
    // linked list
    if (head == NULL) {
        Node* newNode = new Node(d);
        head = newNode;
        tail = newNode;
        newNode->next = newNode;
    }
    else {
 
        // Non-empty list
        Node* temp = new Node(d);
        temp->next = tail->next;
        tail->next = temp;
        tail = tail->next;
    }
}
 
// Function to print circular linked list
void print(Node* head)
{
    Node* curr = head;
 
    // If circular linked list is empty
    if (head == NULL) {
        cout << "List is Empty " << endl;
        return;
    }
 
    // Else iterate until node is NOT head
    do {
        cout << curr->data << " ";
        curr = curr->next;
    } while (curr != head);
    cout << endl;
}
 
string convertString(Node* head)
{
    Node* curr = head;
    string s = "";
 
    // If circular linked list is empty
    if (head == NULL) {
        return "";
    }
 
    // Else iterate until node is NOT head
    do {
        s += to_string(curr->data);
        s += "*";
        curr = curr->next;
    } while (curr != head);
 
    return s;
}
 
// Function to Check that two circular
// linked list are identical or not
bool checkIdentical(string& S, string& T)
{
    /* Check if sizes of two strings are same */
    if (S.length() != T.length())
        return false;
 
    string temp = S + S;
    return (temp.find(T) != string::npos);
}
 
// Driver Code
int main()
{
 
    Node* head1 = NULL;
    Node* tail1 = NULL;
 
    insertNode(head1, tail1, 1);
    insertNode(head1, tail1, 2);
    insertNode(head1, tail1, 3);
    insertNode(head1, tail1, 4);
    insertNode(head1, tail1, 5);
    insertNode(head1, tail1, 1);
    insertNode(head1, tail1, 2);
    insertNode(head1, tail1, 6);
 
    Node* head2 = NULL;
    Node* tail2 = NULL;
 
    insertNode(head2, tail2, 5);
    insertNode(head2, tail2, 1);
    insertNode(head2, tail2, 2);
    insertNode(head2, tail2, 6);
    insertNode(head2, tail2, 1);
    insertNode(head2, tail2, 2);
    insertNode(head2, tail2, 3);
    insertNode(head2, tail2, 4);
 
    cout << "First circular linked list: ";
    print(head1);
 
    cout << "Second circular linked list: ";
    print(head2);
 
    string S = convertString(head1);
    string T = convertString(head2);
 
    bool flag = checkIdentical(S, T);
    if (flag)
        cout << "Identical";
    else
        cout << "Not Identical";
    return 0;
}


Output

First circular linked list: 1 2 3 4 5 1 2 6 
Second circular linked list: 5 1 2 6 1 2 3 4 
Identical

Time Complexity: O(N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!