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 Convert a given Singly Linked List to a Circular List
A singly linked list can be converted to a circular linked list by making the last node point to the head node instead of None. This creates a circular structure where traversal can continue indefinitely.
Node and LinkedList Structure
First, we define the basic node and linked list classes ?
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList_struct:
def __init__(self):
self.head = None
self.last_node = None
def add_elements(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
# Create linked list and add elements
my_list = LinkedList_struct()
elements = [56, 32, 11, 45, 90, 87]
for data in elements:
my_list.add_elements(data)
print("Singly linked list created with elements:", elements)
Singly linked list created with elements: [56, 32, 11, 45, 90, 87]
Converting to Circular List
The conversion function makes the last node point to the head node ?
def convert_to_circular_list(linked_list):
if linked_list.last_node:
linked_list.last_node.next = linked_list.head
def check_last_node_points(linked_list):
last = linked_list.last_node
if last is None:
print('The list is empty...')
return
if last.next is None:
print('The last node points to None...')
else:
print('The last node points to element that has {}...'.format(last.next.data))
# Check before conversion
check_last_node_points(my_list)
print('Converting to circular linked list...')
convert_to_circular_list(my_list)
# Check after conversion
check_last_node_points(my_list)
The last node points to None... Converting to circular linked list... The last node points to element that has 56...
Complete Example
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList_struct:
def __init__(self):
self.head = None
self.last_node = None
def add_elements(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 convert_to_circular_list(linked_list):
if linked_list.last_node:
linked_list.last_node.next = linked_list.head
def check_last_node_points(linked_list):
last = linked_list.last_node
if last is None:
print('The list is empty...')
return
if last.next is None:
print('The last node points to None...')
else:
print('The last node points to element that has {}...'.format(last.next.data))
# Create and populate linked list
my_list = LinkedList_struct()
elements = [56, 32, 11, 45, 90, 87]
for data in elements:
my_list.add_elements(data)
print("Before conversion:")
check_last_node_points(my_list)
print('\nConverting to circular linked list...')
convert_to_circular_list(my_list)
print("After conversion:")
check_last_node_points(my_list)
Before conversion: The last node points to None... Converting to circular linked list... After conversion: The last node points to element that has 56...
How It Works
The
Nodeclass represents individual elements with data and next pointerThe
LinkedList_structclass maintains head and last_node referencesThe
add_elementsmethod adds new nodes to the end of the listThe
convert_to_circular_listfunction setslast_node.next = headThe conversion creates a circular structure where the last node points back to the first
Conclusion
Converting a singly linked list to circular requires only one step: make the last node point to the head node. This creates a circular structure useful for round-robin algorithms and continuous traversals.
