The problem is to reverse the given doubly circular linked list.
insertEnd(head, new_node) Declare last if head == NULL then new_node->next = new_node->prev = new_node head = new_node return last = head->prev new_node->next = head head->prev = new_node new_node->prev = last last->next = new_node reverse(head) Initialize new_head = NULL Declare last last = head->prev Initialize curr = last, prev while curr->prev != last prev = curr->prev insertEnd(&new_head, curr) curr = prev insertEnd(&new_head, curr) return new_head
Explanation: The variable head in the parameter list of insertEnd() is pointer to a pointer variable. reverse() traverses the doubly circular linked list starting with head pointer in backward direction and one by one gets the node in the traversal. It inserts those nodes at the end of the list that starts with the new_head pointer with the help of the function insertEnd() and finally returns new_head.
Current list: Forward direction: 1 2 3 4 5 Backward direction: 5 4 3 2 1 Reversed list: Forward direction: 5 4 3 2 1 Backward direction: 1 2 3 4 5
Time Complexity: O(n).
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.
- Doubly Circular Linked List | Set 1 (Introduction and Insertion)
- Doubly Circular Linked List | Set 2 (Deletion)
- Convert a given Binary Tree to Circular Doubly Linked List | Set 2
- Insertion at Specific Position in a Circular Doubly Linked List
- Convert an Array to a Circular Doubly Linked List
- Search an Element in Doubly Circular Linked List
- Remove all even parity nodes from a Doubly and Circular Singly Linked List
- 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
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Reverse a Doubly Linked List
- Reverse a Doubly linked list using recursion
- Reverse a Doubly Linked List | Set-2
- Reverse a Doubly Linked List | Set 4 (Swapping Data)
- Reverse a doubly linked list in groups of given size
- Print Doubly Linked list in Reverse Order
- Check if a linked list is Circular Linked List
- Convert singly linked list into circular linked list
- Reverse a circular 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 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.