Given a doubly-linked list and an integer N, the task is to rotate the linked list clockwise by N nodes.
Input: N = 2
Approach: To rotate the Doubly linked list first check whether the given N is greater than the length of the list or not. If N is greater than the size of the list then deduce it in the range of linked list size by taking modulo with the length of the list. After that subtract the value of N from the length of the list. Now the problem reduces to the counter-clockwise rotation of a doubly-linked list by N places.
- Change the next of the last node to point the Head node.
- Change the prev of the Head node to point the last node.
- Change the value of the Head_ref to be the next of the Nth node.
- Change the value of next of the Nth Node to be NULL.
- Finally make the prev of Head node to point to NULL.
Below is the implementation of the above approach:
Given Doubly linked list a <=> b <=> c <=> d <=> e Rotated Linked list clockwise d <=> e <=> a <=> b <=> c
Time Complexity: O(n) where n is the number of nodes in Linked List.
- Clockwise rotation of Linked List
- Rotate the sub-list of a linked list from position M to N to the right by K places
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Difference between Singly linked list and Doubly linked list
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Reverse a Doubly Linked List | Set-2
- QuickSort on Doubly Linked List
- Reverse a Doubly Linked List
- Implementation of Deque using doubly linked list
- Reverse a Doubly linked list using recursion
- Insertion Sort for Doubly Linked List
- Rotate Doubly linked list by N nodes
- Sort the biotonic doubly linked list | Set-2
- Python | Stack using Doubly Linked List
- Python | Queue using Doubly Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.