https://www.geeksforgeeks.org/linked-list-in-zig-zag-fashion/
https://www.geeksforgeeks.org/rearrange-a-linked-list-in-zig-zag-fashion-set-2/
X. https://www.geeksforgeeks.org/convert-array-into-zig-zag-fashion/
Given a linked list, rearrange it such that converted list should be of the form a < b > c < d > e < f .. where a, b, c.. are consecutive data node of linked list. Examples :
Input: 1->2->3->4 Output: 1->3->2->4
A simple approach to do this, is to sort the linked list using merge sort and then swap alternate, but that requires O(n Log n) time complexity. Here n is number of elements in linked list.
An efficient approach which requires O(n) time is, using a single scan similar to bubble sort and then maintain a flag for representing which order (< or >) currently we are. If the current two elements are not in that order then swap those elements otherwise not. Please refer this for detailed explanation of swapping order.
A solution that converts given list into zigzag form is discussed in previous post. The solution discussed performs conversion by swapping data of nodes. Swapping data of nodes may be expensive in many situations when the data contains many fields. In this post, a solution that performs conversion by swapping links is discussed.
The idea is to traverse the given linked list and check if current node maintains the zigzag order or not. To check if given node maintains zigzag order or not, a variable ind is used. If ind = 0, then the current node’s data should be less than its adjacent node’s data and if ind = 1, then current node’s data should be greater than its adjacent node’s data. If the current node violates the zigzag order, then swap the position of both nodes. For doing this step, maintain two pointers prev and next. prev stores previous node of current node and next stores new next node of current node. To swap both nodes, the following steps are performed:
- Make next node of current node, the next node of previous node.
- Make the current node next node of its adjacent node.
- Make current node next = next node.
X. https://www.geeksforgeeks.org/convert-array-into-zig-zag-fashion/
Given an array of DISTINCT elements, rearrange the elements of array in zig-zag fashion in O(n) time. The converted array should be in form a < b > c < d > e < f.
Example: Input: arr[] = {4, 3, 7, 8, 6, 2, 1} Output: arr[] = {3, 7, 4, 8, 2, 6, 1}
We can convert in O(n) time using an Efficient Approach. The idea is to use modified one pass of bubble sort. Maintain a flag for representing which order(i.e. < or >) currently we need. If the current two elements are not in that order then swap those elements otherwise not.
Let us see the main logic using three consecutive elements A, B, C. Suppose we are processing B and C currently and the current relation is ‘<'. But we have B > C. Since current relation is ‘<' previous relation must be '>‘ i.e., A must be greater than B. So, the relation is A > B and B > C. We can deduce A > C. So if we swap B and C then the relation is A > C and C < B. Finally we get the desired order A C B
Let us see the main logic using three consecutive elements A, B, C. Suppose we are processing B and C currently and the current relation is ‘<'. But we have B > C. Since current relation is ‘<' previous relation must be '>‘ i.e., A must be greater than B. So, the relation is A > B and B > C. We can deduce A > C. So if we swap B and C then the relation is A > C and C < B. Finally we get the desired order A C B