Check if two Circular Linked Lists are identical
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: Yes
Explanatin: 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: No
Approach: The problem can be solved by traversing the circular linked list using the following idea:
Fix the starting point of any list. Now consider every element of the other list as head and compare if both the lists are identical or not for that starting point.
Follow the steps to mentioned below to solve the problem:
- Calculate, length l1 and l2 of both the circular linked lists respectively
- If both the length are different then return false
- Initialize Count = 0, flag = false
- Initialize temporary pointers, h1 and h2 for head1 (starting pointer of l1) and head2 (starting pointer of l2)
- Traverse while it does not return a bool, true or false:
- If, data of h1 is equal to data of h2 then shift the h1 and h2 to its next node and increse the count by 1.
- If Count equals to l1 (or l2) then linked lists are identical, return true
- Else, reset the pointer h1 and variable count to their initial state.
- If, flag == 1, return false as it means one rotation is completed, and now if element does not matches then list can not be identical.
- If, h2->next = head2 then one rotation is complete, then set, flag = 1
- Move h2 pointer by 1 position, h2 = h2->next.
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 Classclass 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 listvoid 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 listvoid 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;} // Function to return length of// circular linked listint length(Node* head){ // If circular linked list is empty if (head == NULL) { return 0; } int size = 0; Node* curr = head; // Else iterate until node is NOT head do { curr = curr->next; size++; } while (curr != head); return size;} // Function to Check that two circular// linked list are identical or notbool checkIdentical(Node*& head1, Node*& head2){ // Get the length of first linked list int l1 = length(head1); // Get the length of first linked list int l2 = length(head2); // If l1!=l2 then linked list can not // be identical if (l1 != l2) return false; // Initialize the variables int Count = 0; bool flag = 0; // Initialize temporary pointers Node* h1 = head1; Node* h2 = head2; // Traverse the list while (1) { // If element matches in two // circular linked list if (h1->data == h2->data) { h1 = h1->next; Count++; // If count equals to l1(or l2) // then linked list are identical if (Count == l1) return true; } // If element does not matches // in two circular linked list else { h1 = head1; Count = 0; // If flag becomes 1 then one // rotation is complete and // if now data does not match then // linked lists are not identical if (flag) return false; } // Check if h2 complete one rotation if (h2->next == head2) flag = 1; // Move h2 to h2->next h2 = h2->next; }} // Driver Codeint 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); bool flag = checkIdentical(head1, head2); if (flag) cout << "Yes"; else cout << "No"; return 0;} |
Yes
Time Complexity: O(N)
Auxiliary Space: O(1)

