
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to rotate doubly linked list by N nodes
When it is required to rotate a doubly linked list by a specific number of nodes, a ‘Node’ class needs to be created. In this class, there are three attributes, the data that is present in the node, the access to the next node of the linked list, and the access to the previous node of the linked list.
Below is a demonstration for the same −
Example
class Node: def __init__(self, my_data): self.previous = None self.data = my_data self.next = None class double_list: def __init__(self): self.head = None self.tail = None self.size = 0 def add_data(self, my_data): new_node = Node(my_data) if(self.head == None): self.head = self.tail = new_node self.head.previous = None self.tail.next = None else: self.tail.next = new_node new_node.previous = self.tail self.tail = new_node self.tail.next = None self.size = self.size + 1; def print_it(self): curr = self.head if (self.head == None): print("The list is empty") return print("The nodes in the doubly linked list are :") while curr != None: print(curr.data) curr = curr.next def rotate_list(self, num): curr = self.head; if(num == 0 or num >= self.size): return; else: for i in range(1, num): curr = curr.next; self.tail.next = self.head; self.head = curr.next; self.head.previous = None; self.tail = curr; self.tail.next = None; my_instance = double_list() print("Elements are being added to the doubly linked list") my_instance.add_data(10) my_instance.add_data(24) my_instance.add_data(54) my_instance.add_data(77) my_instance.add_data(24) my_instance.add_data(0) my_instance.print_it() print("The elements in the list after rotating : ") my_instance.rotate_list(4) my_instance.print_it()
Output
Elements are being added to the doubly linked list The nodes in the doubly linked list are : 10 24 54 77 24 0 The elements in the list after rotating : The nodes in the doubly linked list are : 24 0 10 24 54 77
Explanation
- The ‘Node’ class is created.
- Another 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 ‘rotate_list’ is defined, that uses the specific node as pivot and rotates the list, thereby moving the elements to a different position.
- Another method named ‘print_it’ is defined that is used to display the linked list data on the console.
- An object of the ‘double_list’ class is created, and the methods are called on it to add data.
- The ‘rotate_list’ method is called.
- It iterates through the nodes in the linked list, gets the middle most index and starts deleting the elements.
- This is displayed on the console using the ‘print_it’ method.
- Related Articles
- Python program to create a doubly linked list of n nodes and count the number of nodes
- Python program to create a doubly linked list of n nodes and display it in reverse order
- Python program to remove duplicate elements from a Doubly Linked List\n
- Python program to right rotate a list by n
- Program to delete n nodes after m nodes from a linked list in Python
- Golang program to count the number of nodes in a doubly linked list.
- Python program to create and display a doubly linked list
- C++ Program to Implement Doubly Linked List
- Python program to create a Circular Linked List of N nodes and count the number of nodes
- Python program to search an element in a doubly linked list
- Program to rotate a linked list by k places in C++
- C++ Program to Implement Circular Doubly Linked List
- C++ Program to Implement Sorted Doubly Linked List
- Delete all Prime Nodes from a Doubly Linked List in C++
- Python program to convert a given binary tree to doubly linked list

Advertisements