- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 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.
To display the data elements in the circular list in reverse order, another method can be defined, that would reverse the data. In this class, there are two attributes, the data that is present in the node, and the access to the next node of the linked list. In a circular linked list, the head and the rear are adjacent to each other. They are connected to form a circle, and don't have 'NULL' value in the last node.
Another 'linked_list' class needs to be created that would have an initialization function, and the head of the node would be initialized to 'None'.
Below is a demonstration for the same −
class Node: def __init__(self, my_data): self.data = my_data self.next = None class linked_list: 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 = current.next current.next = prev prev = current current = next self.head = prev def print_it(self): temp = self.head while(temp): print(temp.data) temp = temp.next my_list = linked_list() 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
- The 'Node' class is created.
- Another 'linked_list' class with required attributes is created.
- Another method named 'add_data' is defined, that is used to add data to the circular linked list.
- Another method named 'reverse' is defined, that reverses the data stored in a circular linked list.
- Another method named 'print_it' is defined that is used to display the linked list data on the console.
- An object of the 'linked_list' class is created, and the data is added to it.
- It is reversed using the 'reverse' method.
- This is displayed on the console using the 'print_it' method.
- Python program to create a doubly linked list of n nodes and display it in reverse order
- Python program to create a Circular Linked List of N nodes and count the number of nodes
- Python program to create and display a Circular Linked List
- Python Program to Display the Nodes of a Linked List in Reverse using Recursion
- Python program to create a doubly linked list of n nodes and count the number of nodes
- Program to reverse inner nodes of a linked list in python
- Python Program to Display the Nodes of a Linked List in Reverse without using Recursion
- Python Circular Linked List Program
- Python program to create and display a doubly linked list
- C program to display numbers in reverse order using single linked list
- Python Program to Reverse only First N Elements of a Linked List
- Program to delete n nodes after m nodes from a linked list in Python
- Python program to rotate doubly linked list by N nodes
- Sum of the nodes of a Circular Linked List in C++
- Delete N nodes after M nodes of a linked list in C++ program