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 Node class represents individual elements with data and next pointer

  • The LinkedList_struct class maintains head and last_node references

  • The add_elements method adds new nodes to the end of the list

  • The convert_to_circular_list function sets last_node.next = head

  • The 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.

Updated on: 2026-03-25T18:56:38+05:30

432 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements