Open In App

Check if a linked list is Circular Linked List

Last Updated : 26 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given the head of a singly linked list, the task is to find if given linked list is circular or not. A linked list is called circular if its last node points back to its first node.

Note: The linked list does not contain any internal loops.

Example:

Input: LinkedList: 2->4->6->7->5

Output: true
Explanation: As shown in figure the first and last node is connected, i.e. 5 -> 2

Input: LinkedList: 2->4->6->7->5->1

Output: false
Explanation: As shown in figure this is not a circular linked list.

[Expected Approach-1] By Traversing each node – O(n) time and O(1) space

The main idea is to traverse through the each node of linked list and check if the head->next pointer points back to the starting node (temp). If it does, that means the linked list is circular.

Code Implementation:

C++
// C++ program to check if linked list is circular
#include <bits/stdc++.h>
using namespace std;

struct Node
{
    int data;
    Node *next;
    Node(int x)
    {
        data = x;
        next = NULL;
    }
};

// This function returns true if given linked
//  list is circular, else false.
bool isCircular(Node *head)
{

    // If the head is null, the linked list is empty,
    // so it is circular
    if (!head)
        return true;

    // Traverse the linked list until either the end
    // is reached or the next node is equal to the head
    Node *curr = head;
    while (curr && curr->next != head)
        curr = curr->next;

    // If the end is reached before finding
    // the head again, the linked list is not circular
    if (!curr)
        return false;

    // If the head is found again before reaching
    // the end, the linked list is circular
    return true;
}

int main()
{
    Node *head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);

    isCircular(head) ? cout << "Yes\n" : cout << "No\n";

    // Making linked list circular
    head->next->next->next->next = head;

    isCircular(head) ? cout << "Yes\n" : cout << "No\n";

    return 0;
}
C Java Python C# JavaScript

Output
No
Yes

Time Complexity: O(n), We traverse the linked list in the worst case once, therefore, the time complexity here is linear.
Auxiliary Space: O(1), We are not using any extra space.

[Expected Approach-2] By Maintaining Slow & Fast Pointers – O(n) time and O(1) space

The idea is to use two pointers, slow and fast, to traverse the linked list. The slow pointer moves one step at a time, while the fast pointer moves two steps at a time. If the list is circular, the fast pointer will eventually meet the slow pointer; otherwise, the fast pointer will reach NULL indicating the list is not circular.

Code Implementation:

C++
// C++ program to check if linked list is circular

#include <bits/stdc++.h>
using namespace std;

struct Node {
    int data;
    struct Node *next;
    Node(int x) {
        data = x;
        next = NULL;
    }
};

// Function to check if the linked list is circular
bool isCircular(Node *head) {
    if (!head) {
        return true;
    }
    Node *slow = head;
    Node *fast = head->next;
    while (fast && fast->next) {
        if (slow == fast) {
            return true;
        }
        slow = slow->next;
        fast = fast->next->next;
    }
    return false;
}

int main() {
    struct Node *head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);

    isCircular(head) ? cout << "Yes\n" : cout << "No\n";

    // Making linked list circular
    head->next->next->next->next = head;

    isCircular(head) ? cout << "Yes\n" : cout << "No\n";

    return 0;
}
C Java Python C# JavaScript

Output
false

Time Complexity: O(n), We traverse the linked list in the worst case once, therefore, the time complexity here is linear.
Auxiliary Space: O(1), We use two Node pointers, slow and fast, so the extra space is constant.



Next Article

Similar Reads

three90RightbarBannerImg