Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to create a Circular Linked List of n nodes and display it in reverse order
When it is required to create a circular linked list and display it in the reverse order, a Node class needs to be created. However, the example below demonstrates a regular linked list implementation, not a true circular linked list.
In a circular linked list, the last node points back to the first node instead of pointing to None, forming a circle. The head and tail are connected to each other. To display the data elements in reverse order, we can either reverse the list structure or traverse it backwards.
Regular Linked List (Current Implementation)
The current code creates a regular linked list where nodes are added at the beginning ?
class Node:
def __init__(self, my_data):
self.data = my_data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def add_data(self, my_data):
new_node = Node(my_data)
new_node.next = self.head
self.head = new_node
def reverse(self):
prev = None
current = self.head
while current is not None:
next_node = current.next
current.next = prev
prev = current
current = next_node
self.head = prev
def print_it(self):
temp = self.head
while temp:
print(temp.data)
temp = temp.next
my_list = LinkedList()
my_list.add_data(47)
my_list.add_data(89)
my_list.add_data(34)
my_list.add_data(11)
print("The list is:")
my_list.print_it()
print("The list is being reversed")
my_list.reverse()
print("The reversed list is:")
my_list.print_it()
The list is: 11 34 89 47 The list is being reversed The reversed list is: 47 89 34 11
True Circular Linked List Implementation
Here's a proper circular linked list where the last node points back to the first ?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def add_data(self, data):
new_node = Node(data)
if not self.head:
self.head = new_node
new_node.next = self.head # Point to itself
else:
# Find the last node
current = self.head
while current.next != self.head:
current = current.next
# Insert at beginning
new_node.next = self.head
current.next = new_node
self.head = new_node
def print_forward(self):
if not self.head:
return
current = self.head
while True:
print(current.data)
current = current.next
if current == self.head:
break
def print_reverse(self, node=None, first_call=True):
if not self.head:
return
if first_call:
node = self.head
# Find next node (or head if we're at the end)
next_node = node.next
# Base case: if next node is head, we've reached the end
if next_node != self.head:
self.print_reverse(next_node, False)
print(node.data)
# Create circular linked list
circular_list = CircularLinkedList()
circular_list.add_data(10)
circular_list.add_data(20)
circular_list.add_data(30)
circular_list.add_data(40)
print("Circular list in forward order:")
circular_list.print_forward()
print("\nCircular list in reverse order:")
circular_list.print_reverse()
Circular list in forward order: 40 30 20 10 Circular list in reverse order: 10 20 30 40
How It Works
The circular linked list implementation includes:
- Node Creation: Each node contains data and a pointer to the next node
- Circular Connection: The last node points back to the head node
- Forward Traversal: Stop when we reach the head node again
- Reverse Display: Use recursion to print nodes in reverse order
Conclusion
A true circular linked list has its tail connected to the head, forming a circle. The recursive approach allows us to display elements in reverse order without modifying the list structure.
