Rearrange a Linked List in 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 Input: 11->15->20->5->10 Output: 11->20->5->15->10
We strongly recommend that you click here and practice it, before moving on to the solution.
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 () 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.
C++
// C++ 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 distributes the Node in zigzag fashion void zigZagList(Node *head) { // If flag is true, then next node should be greater // in the desired output. bool flag = true; // Traverse linked list starting from head. Node* current = head; while (current->next != NULL) { if (flag) /* "<" relation expected */ { /* If we have a situation like A > B > C where A, B and C are consecutive Nodes in list we get A > B < C by swapping B and C */ if (current->data > current->next->data) swap(current->data, current->next->data); } else /* ">" relation expected */ { /* If we have a situation like A < B < C where A, B and C are consecutive Nodes in list we get A < C > B by swapping B and C */ if (current->data < current->next->data) swap(current->data, current->next->data); } current = current->next; flag = !flag; /* flip flag for reverse checking */ } } /* 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; } printf("NULL"); } /* 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); zigZagList(head); printf("\nZig Zag Linked list \n"); printList(head); return (0); } |
Java
// Java program to arrange // linked list in zigzag fashion class GfG { /* Link list Node */static class Node { int data; Node next; } static Node head = null; static int temp = 0; // This function distributes // the Node in zigzag fashion static void zigZagList(Node head) { // If flag is true, then // next node should be greater // in the desired output. boolean flag = true; // Traverse linked list starting from head. Node current = head; while (current != null && current.next != null) { if (flag == true) /* "<" relation expected */ { /* If we have a situation like A > B > C where A, B and C are consecutive Nodes in list we get A > B < C by swapping B and C */ if (current.data > current.next.data) { temp = current.data; current.data = current.next.data; current.next.data = temp; } } else /* ">" relation expected */ { /* If we have a situation like A < B < C where A, B and C are consecutive Nodes in list we get A < C > B by swapping B and C */ if (current.data < current.next.data) { temp = current.data; current.data = current.next.data; current.next.data = temp; } } current = current.next; /* flip flag for reverse checking */ flag = !(flag); } } /* UTILITY FUNCTIONS *//* Function to push a Node */static void push( int new_data) { /* allocate Node */ 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); /* move the head to point to the new Node */ (head) = new_Node; } /* Function to print linked list */static void printList(Node Node) { while (Node != null) { System.out.print(Node.data + "->"); Node = Node.next; } System.out.println("NULL"); } /* Driver code*/public static void main(String[] args) { /* Start with the empty list */ //Node head = null; // create a list 4 -> 3 -> 7 -> 8 -> 6 -> 2 -> 1 // answer should be -> 3 7 4 8 2 6 1 push(1); push(2); push(6); push(8); push( 7); push( 3); push( 4); System.out.println("Given linked list "); printList(head); zigZagList(head); System.out.println("Zig Zag Linked list "); printList(head); } } // This code is contributed by // Prerna Saini. |
C#
// C# program to arrange // linked list in zigzag fashion using System; class GfG { /* Link list Node */ class Node { public int data; public Node next; } static Node head = null; static int temp = 0; // This function distributes // the Node in zigzag fashion static void zigZagList(Node head) { // If flag is true, then // next node should be greater // in the desired output. bool flag = true; // Traverse linked list starting from head. Node current = head; while (current != null && current.next != null) { if (flag == true) /* "<" relation expected */ { /* If we have a situation like A > B > C where A, B and C are consecutive Nodes in list we get A > B < C by swapping B and C */ if (current != null && current.next != null && current.data > current.next.data) { temp = current.data; current.data = current.next.data; current.next.data = temp; } } else /* ">" relation expected */ { /* If we have a situation like A < B < C where A, B and C are consecutive Nodes in list we get A < C > B by swapping B and C */ if (current != null && current.next != null && current.data < current.next.data) { temp = current.data; current.data = current.next.data; current.next.data = temp; } } current = current.next; /* flip flag for reverse checking */ flag = !(flag); } } /* UTILITY FUNCTIONS */ /* Function to push a Node */ static void push( int new_data) { /* allocate Node */ 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); /* move the head to point to the new Node */ (head) = new_Node; } /* Function to print linked list */ static void printList(Node Node) { while (Node != null) { Console.Write(Node.data + "->"); Node = Node.next; } Console.WriteLine("NULL"); } /* Driver code*/ public static void Main() { /* Start with the empty list */ //Node head = null; // create a list 4 -> 3 -> 7 -> 8 -> 6 -> 2 -> 1 // answer should be -> 3 7 4 8 2 6 1 push(1); push(2); push(6); push(8); push( 7); push( 3); push( 4); Console.WriteLine("Given linked list "); printList(head); zigZagList(head); Console.WriteLine("Zig Zag Linked list "); printList(head); } } /* This code is contributed PrinciRaj1992 */ |
Output :
Given linked list 4->3->7->8->6->2->1->NULL Zig Zag Linked list 3->7->4->8->2->6->1->NULL
In above code, push function pushes the node at the front of the linked list, the code can be easily modified for pushing node at the end of list. Other thing to note is, swapping of data between two nodes is done by swap by value not swap by links for simplicity, for swap by links technique please see this.
Time complexity : O(n)
Auxiliary Space : O(1)
This article is contributed by Utkarsh Trivedi. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Recommended Posts:
- Rearrange a Linked List in Zig-Zag fashion | Set-2
- Rearrange a given linked list in-place.
- Rearrange a linked list such that all even and odd positioned nodes are together
- Rearrange a linked list in to alternate first and last element
- Create new linked list from two given linked list with greater element at each node
- Difference between Singly linked list and Doubly linked list
- Merge a linked list into another linked list at alternate positions
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Convert singly linked list into circular linked list
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- Modify and Rearrange List
- Convert Singly Linked List to XOR Linked List
- Check if a linked list is Circular Linked List
- Rearrange a given list such that it consists of alternating minimum maximum elements
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes

