Rearrange a Linked List in Zig-Zag fashion | Set-2

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. Note that it is not allowed to swap data.

Examples:

Input:  1->2->3->4
Output: 1->3->2->4 

Input:  11->15->20->5->10
Output: 11->20->5->15->10


Approach:
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.

Below is the implementation of above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to arrange linked list in
// zigzag fashion
#include <bits/stdc++.h>
using namespace std;
  
/* Link list Node */
struct Node {
    int data;
    struct Node* next;
};
  
// This function converts the Linked list in
// zigzag fashion
Node* zigZagList(Node* head)
{
    if (head == NULL || head->next == NULL) {
        return head;
    }
  
    // to store new head
    Node* res = NULL;
  
    // to traverse linked list
    Node* curr = head;
  
    // to store previous node of current node
    Node* prev = NULL;
  
    // to store new next node of current node
    Node* next;
  
    // to check if current element should
    // be less than or greater than.
    // ind = 0 --> less than
    // ind = 1 --> greater than
    int ind = 0;
  
    while (curr->next) {
  
        // If elements are not in zigzag fashion
        // swap them.
        if ((ind == 0 && curr->data > curr->next->data)
            || (ind == 1 && curr->data < curr->next->data)) {
  
            if (res == NULL)
                res = curr->next;
  
            // Store new next element of current
            // node
            next = curr->next->next;
  
            // Previous node of current node will
            // now point to next node of current node
            if (prev)
                prev->next = curr->next;
  
            // Change next pointers of both
            // adjacent nodes
            curr->next->next = curr;
            curr->next = next;
  
            // Change previous pointer.
            if (prev)
                prev = prev->next;
            else
                prev = res;
        }
  
        // If already in zig zag form, then move
        // to next element.
        else {
            if (res == NULL) {
                res = curr;
            }
  
            prev = curr;
            curr = curr->next;
        }
  
        // Update info whether next element should
        // be less than or greater than.
        ind = 1 - ind;
    }
  
    return res;
}
  
/* UTILITY FUNCTIONS */
/* Function to push a Node */
void push(Node** head_ref, int new_data)
{
    /* allocate Node */
    struct Node* new_Node = new Node;
  
    /* put in the data */
    new_Node->data = new_data;
  
    /* link the old list off the new Node */
    new_Node->next = (*head_ref);
  
    /* move the head to point to the new Node */
    (*head_ref) = new_Node;
}
  
/* Function to print linked list */
void printList(struct Node* Node)
{
    while (Node != NULL) {
        printf("%d->", Node->data);
        Node = Node->next;
    }
}
  
/* Driver program to test above function*/
int main(void)
{
    /* Start with the empty list */
    struct Node* head = NULL;
  
    // create a list 4 -> 3 -> 7 -> 8 -> 6 -> 2 -> 1
    // answer should be -> 3 7 4 8 2 6 1
    push(&head, 1);
    push(&head, 2);
    push(&head, 6);
    push(&head, 8);
    push(&head, 7);
    push(&head, 3);
    push(&head, 4);
  
    printf("Given linked list \n");
    printList(head);
  
    head = zigZagList(head);
  
    printf("\nZig Zag Linked list \n");
    printList(head);
  
    return 0;
}

chevron_right


Output:

Given linked list 
4->3->7->8->6->2->1->
Zig Zag Linked list 
3->7->4->8->2->6->1->

Time Complexity: O(N)
Auxiliary Space: O(1)



My Personal Notes arrow_drop_up

A Programmer and A Machine learning Enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.