Given a linked list. arrange the linked list in manner of alternate first and last element.
Input : 1->2->3->4->5->6->7->8 Output :1->8->2->7->3->6->4->5 Input :10->11->15->13 Output :10->13->11->15
We have discussed three different solution in Rearrange a given linked list in-place.
In this post a different Deque based solution is discussed.
1) Create an empty deque
2) Insert all element from the linked list to the deque
3) Insert the element back to the linked list from deque in alternate fashion i.e first then last and so on
Given linked list 1 2 3 4 5 After rearrangement 1 5 2 4 3
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.
- Merge a linked list into another linked list at alternate positions
- Exchange first and last nodes in Circular Linked List
- Given a linked list, reverse alternate nodes and append at the end
- Alternate Odd and Even Nodes in a Singly Linked List
- Rearrange a linked list such that all even and odd positioned nodes are together
- Create new linked list from two given linked list with greater element at each node
- Move last element to front of a given Linked List
- Move last element to front of a given Linked List | Set 2
- Delete alternate nodes of a Linked List
- Reverse alternate K nodes in a Singly Linked List
- Alternate sorting of Linked list
- Print alternate nodes of a linked list using recursion
- Print the alternate nodes of linked list (Iterative Method)
- Product of the alternate nodes of linked list
- Sum of the alternate nodes of linked list
- Reverse alternate K nodes in a Singly Linked List - Iterative Solution
- Subtraction of the alternate nodes of Linked List
- Rearrange a given linked list in-place.
- Rearrange a Linked List in Zig-Zag fashion
- Rearrange a Linked List in Zig-Zag fashion | Set-2
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.