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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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 the sub-list of a linked list from position M to N to the right by K places
- Delete all the nodes from the doubly linked list that are greater than a given value
- Delete all the even nodes from a Doubly Linked List
- Product of all prime nodes in a Doubly Linked List
- Delete all the nodes from a doubly linked list that are smaller than a given value
- Sum of all nodes in a doubly linked list divisible by a given number K
- Replace even nodes of a doubly linked list with the elements of array
- Delete all Prime Nodes from a Doubly Linked List
- Delete all nodes from the doubly linked list which are divisible by K
- Product of all nodes in a doubly linked list divisible by a given number K
- Remove all nodes from a Doubly Linked List containing Fibonacci numbers
- Remove all even parity nodes from a Doubly and Circular Singly Linked List
- Remove all the Even Digit Sum Nodes from a Doubly Linked List
- Construct a Doubly linked linked list from 2D Matrix
- Generate Linked List consisting of maximum difference of squares of pairs of nodes from given Linked List
- Rotate Linked List block wise
- Rotate a 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.