Linked List in Zig-Zag fashion
Given a Linked list, rearrange it such that converted list should be of the from a < b > c < d > e < f .. where a, b, c are consecutive data node of linked list and such that the order of linked list is sustained.
In 11 15 20 5 10 we consider only 11 20 5 15 10 because it satisfies the above condition and the order of linked list. 5 20 11 15 10 is not considered as it does not follow the order of linked list.
- If list is empty or list has only one child then return
mode = zig
- Iterate till
node.next != null
(mode == zig and node.data > node.next.data)then swap node.data with node.next.data
(mode == zag and node.data < node.next.data)then swap node.data with node.next.data
mode = (mode== zig) ? zag : zig
Latest Source Code:
11 15 20 5 10 11 20 5 15 10