Search⌘ K

Solution Review: Is Circular Linked List

Explore the method to identify if a linked list is circular by traversing nodes and checking link conditions. Understand the is_circular_linked_list function with a practical Python implementation and testing examples. This lesson helps you master the detection of circular linked lists as part of learning different linked list structures.

We'll cover the following...

In this lesson, we investigate how to determine whether a given linked list is either a singly linked list or a circular linked list.

Implementation

Have a look at the coding solution below:

Python 3.5
def is_circular_linked_list(self, input_list):
if input_list.head:
cur = input_list.head
while cur.next:
cur = cur.next
if cur.next == input_list.head:
return True
return False
else:
return False

Explanation

Let’s discuss the class method is_circular_linked_list. First, we check whether or not parameter is empty. ...