Given a doubly linked list, rotate the linked list counter-clockwise by N nodes. Here N is a given positive integer and is smaller than the count of nodes in linked list.
N = 2
Input : a b c d e N = 2 Output : c d e a b Input : a b c d e f g h N = 4 Output : e f g h a b c d
Asked in Amazon
To rotate the Doubly linked list, we need to change next of Nth node to NULL, next of last node to previous head node, and prev of head node to last node and finally change head to (N+1)th node and prev of new head node to NULL (Prev of Head node in doubly linked list is NULL)
So we need to get hold of three nodes: Nth node, (N+1)th node and last node. Traverse the list from beginning and stop at Nth node. Store pointer to Nth node. We can get (N+1)th node using NthNode->next. Keep traversing till end and store pointer to last node also. Finally, change pointers as stated above and at Last Print Rotated List using
Given linked list a b c d e Rotated Linked list c d e a b
- Delete all the even nodes from a Doubly Linked List
- Product of all prime nodes in a Doubly Linked List
- Delete all Prime Nodes from a Doubly Linked List
- Delete all the nodes from the doubly linked list that are greater than a given value
- Sum of all nodes in a doubly linked list divisible by a given number K
- Delete all nodes from the doubly linked list which are divisible by K
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Replace even nodes of a doubly linked list with the elements of array
- Product of all nodes in a doubly linked list divisible by a given number K
- 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
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Difference between Singly linked list and Doubly linked list
- Rotate a Linked List
- Rotate Linked List block wise
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.