- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Detect the Cycle in a Linked List
When it is required to detect a cycle in a linked list, a method to add elements to the linked list, and a method to get the element in the linked list are defined. Another method is defined that checks if the head and rear values are same or not. Based on this result, cycles are detected.
Below is a demonstration for the same −
class Node: def __init__(self, data): self.data = data self.next = None class LinkedList_structure: def __init__(self): self.head = None self.last_node = None def add_vals(self, data): if self.last_node is None: self.head = Node(data) self.last_node = self.head else: self.last_node.next = Node(data) self.last_node = self.last_node.next def get_node_val(self, index): curr = self.head for i in range(index): curr = curr.next if curr is None: return None return curr def check_cycle(my_list): slow_val = my_list.head fast_val = my_list.head while (fast_val != None and fast_val.next != None): slow_val = slow_val.next fast_val = fast_val.next.next if slow_val == fast_val: return True return False my_linked_list = LinkedList_structure() my_list = input('Enter the elements in the linked list ').split() for elem in my_list: my_linked_list.add_vals(int(elem)) my_len = len(my_list) if my_len != 0: vals = '0-' + str(my_len - 1) last_ptr = input('Enter the index [' + vals + '] of the node' ' at which the last node has to point'' (Enter nothing to point to None): ').strip() if last_ptr == '': last_ptr = None else: last_ptr = my_linked_list.get_node_val(int(last_ptr)) my_linked_list.last_node.next = last_ptr if check_cycle(my_linked_list): print("The linked list has a cycle") else: print("The linked list doesn't have a cycle")
Enter the elements in the linked list 56 78 90 12 4 Enter the index [0-4] of the node at which the last node has to point (Enter nothing to point to None): The linked list doesn't have a cycle
The ‘Node’ class is created.
Another ‘LinkedList_structure’ class with required attributes is created.
It has an ‘init’ function that is used to initialize the first element, i.e the ‘head’ to ‘None’.
A method named ‘add_vals’ is defined, that helps add a value to the stack.
Another method named ‘get_node_val’ is defined, that helps get the value of the current node in the linked list.
Another method named ‘check_cycle’ is defined, that helps find if the head and rear are same, which means this would be a cycle.
It returns True or False depending on the presence or absence of cycle.
An instance of the ‘LinkedList_structure’ is created.
Elements are added to the linked list.
The ‘check_cycle’ method is called on this linked list.
The output is displayed on the console.
Kickstart Your Career
Get certified by completing the courseGet Started