Given a linked list with n nodes, reverse it in the following way :
- If n is even, reverse it in group of n/2 nodes.
- If n is odd, keep the middle node as it is, reverse first n/2 elements and reverse last n/2 elements.
Input : 1 2 3 4 5 6 (n is even)
Output : 3 2 1 6 5 4
Input : 1 2 3 4 5 6 7 (n is odd)
Output : 3 2 1 4 7 6 5
Approach: The idea is similar to Reversing a linked list in groups of size k where k is n/2. Just need to check for mid node.
- If n is even, divide the linked list into two parts i.e. first n/2 elements and last n/2 elements and reverse both the parts.
- If n is odd, divide the linked list into three parts i.e. first n/2 elements, (n/2 + 1) th element and last n/2 elements and reverse both the parts except (n/2 + 1) th element .
Original List : 1 2 3 4 5 6 7 8 9 Reversed List : 4 3 2 1 5 9 8 7 6
- Reverse each word in a linked list node
- Reverse a Linked List in groups of given size | Set 1
- Delete all nodes from the doubly linked list which are divisible by K
- Check if two Linked Lists are permutations of each other
- Delete all Prime Nodes from a Doubly Linked List
- Print Doubly Linked list in Reverse Order
- Sort Linked List containing values from 1 to N
- Find the Second Largest Element in a Linked List
- Sort a Linked List in wave form
- Check if a Linked List is Pairwise Sorted
- Check if elements of Linked List are present in pair
- Delete all the nodes from the list which are divisible by any given number K
- Replace even nodes of a doubly linked list with the elements of array
- Sandvine Interview Experience for FTE 2018
- Delete all the even nodes from a 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.