Check if a Given Linked List is Circular Linked List17 Mar 2025 | 6 min read In this tutorial, we will write the Python program to check whether a given linked list is a circular linked list or not. We will understand the various efficient methods to determine a circular linked list. It is understood that you are familiar with the basic concept of the linked list and the Circular linked list (can skip introduction part). If you are not familiar with linked list, let's briefly overview the linked list. What is Linked List?The linked list is a linear data structure that stores the address of the next element. Each element is known as node, and a node consists of data and the memory address of the next element. We can access the linked list's element through the pointer, and the first node is known as the head. A linked list provides the advantage over the array because it stores the element dynamically where the array has a fixed size. In the linked list, we can insert and delete the element easily. Circular Linked ListA circular linked list is one type of linked list which last following pointer field has a reference to one of the elements in the list. On the other hand, the next pointer field of the pointer element has the null value. A loop exists in a linked list when no NULL is reached as we traverse the entire linked list. ![]() How can we check the Given Linked list is Circular?As per the definition of both linked lists, we can find out easily when we traverse the linked list.
Let's understand the solution. Solution - 1:Follow the below steps to check whether a given list is a circular or not.
Let's understand the below code. Example - Output: The given linked list is not a circular list The given linked list is a circular list Explanation - In the above code, we have created a Node class to initialize the node of the linked list. Then we created a LinkedList class that will point to the head node of the linked list; initially, it is none. Finally, we defined the CircularList function that determines whether a given linked list is circular or not. In this function, we checked if a head is equal to none. If true, then it returns True. If the head is not none, assign the head's next element to the current and set i as zero. Then we define the while loop, which will run until the current is not none and the current is not head. If the loop terminates the conditions, the return current is equal to the head. Solution -2: Two Pointer SolutionWe can use the two pointers with the different speed to determine whether a given linked list is a circular linked list or not. We define a slow pointer and a fast pointer. We use these pointers to traverse the linked list. The slow pointer moves one step at a time and a fast pointer moves two steps at a time. If a linked list is not a circular, the fast pointer will reach at the end of the element whose next pointer is null. Let's understand the approach.
Let's implement the above approach into the Python code. Example - Output: The Given list is a circular list Explanation - We have created the Node and LinkedList classes to initialize the node and head in the above code. Then we created the circular_list() function, which takes the head as an argument. There is no linked list if the head is none; otherwise, we assigned a slow pointer as the head and a fast pointer as the head next element. Then iterate the linked list if the till slow is not equal to the fast. In the loop, we checked fast pointer is None or the fast's next element is None; if the condition is true, we returned the linked list is not a circular linked list. Otherwise, we assigned slow.next element to slow pointer and fast.next.next element to the fast pointer. If the slow and fast pointer became equal, it means the given linked list is a circular linked list. The loop is finished and returns true to indicate a circular linked list. Complexity Analysis of the Two Pointer SolutionIf the linked list doesn't have the cycle, the fast pointer will reach the end. Therefore the time complexities will be the O (n) in this case. If the linked list has a cycle, we need to calculate the number of steps to make the fast pointer catch the slow pointer. Let's understand the movement of both pointers. The slow pointer requires K steps to enter the cycle where the fast pointer has already in the cycle and element apart from the slow pointer in the linked list direction. The total run time will be 0 (K+D), where K is the number of elements between the head and the start element of the cycle. The D represents the distance between the two pointers when the slow pointer reaches the cycle. The space complexity is O (1). ConclusionIn this tutorial, we have learned about how we can check a linked list is a circular linked list. We have discussed the two solutions, the first is a brute-force approach, and the second is a two-pointer solution. The two-pointer solution is easy to implement and also more efficient. Next TopicReverse the Linked List in Python |
The most adaptable language is Python, which is used in nearly every industry, including game development, web development, machine learning, artificial intelligence, and GUI applications. The game is developed using the pygame package, which is a built-in feature of Python. With a rudimentary understanding of Python programming,...
12 min read
In this tutorial, we will learn how to flush the output data buffer explicitly using the flush parameter of the print() function. We will also determine when we need to flush the data buffer and when we don't need it. We will also discuss changing data...
10 min read
You are given a list containing duplicate elements. The goal is to arrange the list of elements in descending order by their frequency of occurrence. For example, suppose we have a list = [2, 2, 2, 2, 3, 1, 1, 1, 4, 4], where each number represents...
8 min read
The problem is a given an integer array, we need to find the kth smallest element in the array where k is a positive integer less than or equal to the length of the array. Let's see the following example. Example - Input: arr = [7, 4, 6, 3,...
5 min read
Artificial Intelligence in cybersecurity is revolutionizing how we look at protecting ourselves against dark-hat hackers and hackers with malicious motives. It's important to take a focused strategy when it comes to cybersecurity. From a technical standpoint, it's important to take a comprehensive approach to security, where...
11 min read
In this tutorial, we will learn how to get amicable numbers in Python. First, we will understand what amicable numbers are and how they can be used. Amicable numbers are two different numbers so related that the sum of the proper divisor of each is equal to...
3 min read
In this tutorial, we will learn to convert our input or output sequence data to a one-hot encoding for use in sequence classification. One Hot Encoding is a useful feature of machine learning because few Machine learning algorithms cannot work with categorical data directly. While working with...
8 min read
Sometimes we found lost in a big repository of Python code and struggled to keep track of the intended types of variables. The type hint and annotation can be helpful to cover the variable types in such scenarios. In this tutorial, we will discuss the annotated...
9 min read
In the following tutorial, we will understand how to read NetCDF Data with the help of the Python programming language. But before we get started, let us briefly understand what exactly NetCDF is. Understanding NetCDF Network common data form (also known as NetCDF) is generally utilized to store multi-dimensional...
7 min read
In this article, we will discuss sentiment analysis in Python. This application proves again that how versatile this programming language is. But before starting sentiment analysis, let us see what is the background that all of us must be aware of- So, here we'll discuss- What is...
5 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India