Given a linked list, rearrange it such that the converted list should be of the form a < b > c < d > e < f … where a, b, c… are consecutive data nodes of the linked list.
Examples:
Input: 1->2->3->4
Output: 1->3->2->4
Explanation : 1 and 3 should come first before 2 and 4 in zig-zag fashion, So resultant linked-list will be 1->3->2->4.
Input: 11->15->20->5->10
Output: 11->20->5->15->10
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 a number of elements in the linked list.
An efficient approach that 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 to this for a detailed explanation of the swapping order.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void zigZagList(Node* head)
{
bool flag = true ;
Node* current = head;
while (current->next != NULL) {
if (flag)
{
if (current->data > current->next->data)
swap(current->data, current->next->data);
}
else
{
if (current->data < current->next->data)
swap(current->data, current->next->data);
}
current = current->next;
flag = !flag;
}
}
void push(Node** head_ref, int new_data)
{
struct Node* new_Node = new Node;
new_Node->data = new_data;
new_Node->next = (*head_ref);
(*head_ref) = new_Node;
}
void printList( struct Node* Node)
{
while (Node != NULL) {
printf ( "%d->" , Node->data);
Node = Node->next;
}
printf ( "NULL" );
}
int main( void )
{
struct Node* head = NULL;
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);
}
|
C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
void swap( int * xp, int * yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void zigZagList(Node* head)
{
bool flag = true ;
Node* current = head;
while (current->next != NULL) {
if (flag)
{
if (current->data > current->next->data)
swap(¤t->data, ¤t->next->data);
}
else
{
if (current->data < current->next->data)
swap(¤t->data, ¤t->next->data);
}
current = current->next;
flag = !flag;
}
}
void push(Node** head_ref, int new_data)
{
struct Node* new_Node = (Node*) malloc ( sizeof (Node));
new_Node->data = new_data;
new_Node->next = (*head_ref);
(*head_ref) = new_Node;
}
void printList( struct Node* Node)
{
while (Node != NULL) {
printf ( "%d->" , Node->data);
Node = Node->next;
}
printf ( "NULL" );
}
int main( void )
{
struct Node* head = NULL;
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
class GfG {
static class Node {
int data;
Node next;
}
static Node head = null ;
static int temp = 0 ;
static void zigZagList(Node head)
{
boolean flag = true ;
Node current = head;
while (current != null && current.next != null ) {
if (flag == true )
{
if (current.data > current.next.data) {
temp = current.data;
current.data = current.next.data;
current.next.data = temp;
}
}
else
{
if (current.data < current.next.data) {
temp = current.data;
current.data = current.next.data;
current.next.data = temp;
}
}
current = current.next;
flag = !(flag);
}
}
static void push( int new_data)
{
Node new_Node = new Node();
new_Node.data = new_data;
new_Node.next = (head);
(head) = new_Node;
}
static void printList(Node Node)
{
while (Node != null ) {
System.out.print(Node.data + "->" );
Node = Node.next;
}
System.out.println( "NULL" );
}
public static void main(String[] args)
{
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);
}
}
|
Python
class Node:
def __init__( self , data):
self .data = data
self . next = None
def zigZagList(head):
flag = True
current = head
while (current. next ! = None ):
if (flag):
if (current.data > current. next .data):
t = current.data
current.data = current. next .data
current. next .data = t
else :
if (current.data < current. next .data):
t = current.data
current.data = current. next .data
current. next .data = t
current = current. next
if (flag):
flag = False
else :
flag = True
return head
def push(head, k):
tem = Node( 0 )
tem.data = k
tem. next = head
head = tem
return head
def display( head):
curr = head
while (curr ! = None ):
print ( curr.data, "->" , end = " " )
curr = curr. next
print ( "None" )
head = None
head = push(head, 1 )
head = push(head, 2 )
head = push(head, 6 )
head = push(head, 8 )
head = push(head, 7 )
head = push(head, 3 )
head = push(head, 4 )
print ( "Given linked list \n" )
display(head)
head = zigZagList(head)
print ( "\nZig Zag Linked list \n" )
display(head)
|
C#
using System;
class GfG {
class Node {
public int data;
public Node next;
}
static Node head = null ;
static int temp = 0;
static void zigZagList(Node head)
{
bool flag = true ;
Node current = head;
while (current != null && current.next != null ) {
if (flag == true )
{
if (current != null && current.next != null && current.data > current.next.data) {
temp = current.data;
current.data = current.next.data;
current.next.data = temp;
}
}
else
{
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;
flag = !(flag);
}
}
static void push( int new_data)
{
Node new_Node = new Node();
new_Node.data = new_data;
new_Node.next = (head);
(head) = new_Node;
}
static void printList(Node Node)
{
while (Node != null ) {
Console.Write(Node.data + "->" );
Node = Node.next;
}
Console.WriteLine( "NULL" );
}
public static void Main()
{
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);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
var head = null ;
var temp = 0;
function zigZagList(head) {
var flag = true ;
var current = head;
while (current != null && current.next != null ) {
if (flag == true )
{
if (current.data > current.next.data) {
temp = current.data;
current.data = current.next.data;
current.next.data = temp;
}
} else
{
if (current.data < current.next.data) {
temp = current.data;
current.data = current.next.data;
current.next.data = temp;
}
}
current = current.next;
flag = !(flag);
}
}
function push(new_data) {
var new_Node = new Node();
new_Node.data = new_data;
new_Node.next = (head);
(head) = new_Node;
}
function printList(node) {
while (node != null ) {
document.write(node.data + "->" );
node = node.next;
}
document.write( "NULL<br/>" );
}
push(1);
push(2);
push(6);
push(8);
push(7);
push(3);
push(4);
document.write( "Given linked list <br/>" );
printList(head);
zigZagList(head);
document.write( "Zig Zag Linked list <br/>" );
printList(head);
</script>
|
OutputGiven linked list
4->3->7->8->6->2->1->NULL
Zig Zag Linked list
3->7->4->8->2->6->1->NULL
Another Approach:
In the above code, the push function pushes the node at the front of the linked list, the code can be easily modified for pushing the node at the end of the list. Another thing to note is, swapping of data between two nodes is done by swap by value not swap by links for simplicity, for the swap by links technique please see this.
This can be also be done recursively. The idea remains the same, let us suppose the value of the flag determines the condition we need to check for comparing the current element. So, if the flag is 0 (or false) the current element should be smaller than the next and if the flag is 1 ( or true ) then the current element should be greater than the next. If not, swap the values of nodes.
Implementation:
C++
#include <iostream>
using namespace std;
struct node {
int data;
node* next;
};
void push(node** head_ref, int new_data)
{
node* new_Node = (node*) malloc ( sizeof (node));
new_Node->data = new_data;
new_Node->next = (*head_ref);
(*head_ref) = new_Node;
}
node* zigzag(node* head, bool flag)
{
if (!head || !head->next)
return head;
if (flag == 1) {
if (head->data > head->next->data)
swap(head->data, head->next->data);
return zigzag(head->next, !flag);
}
else {
if (head->data < head->next->data)
swap(head->data, head->next->data);
return zigzag(head->next, !flag);
}
}
void printList(node* head)
{
while (head) {
cout << head->data << "-> " ;
head = head->next;
}
cout << "NULL" ;
}
int main()
{
node* head = NULL;
push(&head, 10);
push(&head, 5);
push(&head, 20);
push(&head, 15);
push(&head, 11);
printList(head);
cout << endl;
zigzag(head, 1);
cout << "LL in zig zag fashion : " << endl;
printList(head);
return 0;
}
|
C
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node* next;
} node;
void push(node** head_ref, int new_data)
{
node* new_Node = (node*) malloc ( sizeof (node));
new_Node->data = new_data;
new_Node->next = (*head_ref);
(*head_ref) = new_Node;
}
void swap( int * xp, int * yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
node* zigzag(node* head, bool flag)
{
if (!head || !head->next)
return head;
if (flag == 1) {
if (head->data > head->next->data)
swap(&head->data, &head->next->data);
return zigzag(head->next, !flag);
}
else {
if (head->data < head->next->data)
swap(&head->data, &head->next->data);
return zigzag(head->next, !flag);
}
}
void printList(node* head)
{
while (head) {
printf ( "%d-> " , head->data);
head = head->next;
}
printf ( "NULL" );
}
int main()
{
node* head = NULL;
push(&head, 10);
push(&head, 5);
push(&head, 20);
push(&head, 15);
push(&head, 11);
printList(head);
printf ( "\n" );
zigzag(head, 1);
printf ( "LL in zig zag fashion : \n" );
printList(head);
return 0;
}
|
Java
import java.io.*;
class Node {
int data;
Node next;
Node( int data) { this .data = data; }
}
public class GFG {
private Node head;
public void printLL()
{
Node t = head;
while (t != null ) {
System.out.print(t.data + " ->" );
t = t.next;
}
System.out.println();
}
public void swap(Node a, Node b)
{
if (a == null || b == null )
return ;
int temp = a.data;
a.data = b.data;
b.data = temp;
}
public Node zigZag(Node node, int flag)
{
if (node == null || node.next == null ) {
return node;
}
if (flag == 0 ) {
if (node.data > node.next.data) {
swap(node, node.next);
}
return zigZag(node.next, 1 );
}
else {
if (node.data < node.next.data) {
swap(node, node.next);
}
return zigZag(node.next, 0 );
}
}
public static void main(String[] args)
{
GFG lobj = new GFG();
lobj.head = new Node( 11 );
lobj.head.next = new Node( 15 );
lobj.head.next.next = new Node( 20 );
lobj.head.next.next.next = new Node( 5 );
lobj.head.next.next.next.next = new Node( 10 );
lobj.printLL();
int flag = 0 ;
lobj.zigZag(lobj.head, flag);
System.out.println( "LL in zig zag fashion : " );
lobj.printLL();
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
head = None
def printLL():
t = head
while (t ! = None ):
print (t.data, end = " ->" )
t = t. next
print ()
def swap(a, b):
if (a = = None or b = = None ):
return
temp = a.data
a.data = b.data
b.data = temp
def zigZag(node, flag):
if (node = = None or node. next = = None ):
return node
if (flag = = 0 ):
if (node.data > node. next .data):
swap(node, node. next )
return zigZag(node. next , 1 )
else :
if (node.data < node. next .data):
swap(node, node. next )
return zigZag(node. next , 0 )
head = Node( 11 )
head. next = Node( 15 )
head. next . next = Node( 20 )
head. next . next . next = Node( 5 )
head. next . next . next . next = Node( 10 )
printLL()
flag = 0
zigZag(head, flag)
print ( "LL in zig zag fashion : " )
printLL()
|
C#
using System;
public class Node {
public int data;
public Node next;
public
Node( int data)
{
this .data = data;
}
}
public class GFG {
private Node head;
public void printLL()
{
Node t = head;
while (t != null ) {
Console.Write(t.data + " ->" );
t = t.next;
}
Console.WriteLine();
}
public void swap(Node a, Node b)
{
if (a == null || b == null )
return ;
int temp = a.data;
a.data = b.data;
b.data = temp;
}
public Node zigZag(Node node, int flag)
{
if (node == null || node.next == null ) {
return node;
}
if (flag == 0) {
if (node.data > node.next.data) {
swap(node, node.next);
}
return zigZag(node.next, 1);
}
else {
if (node.data < node.next.data) {
swap(node, node.next);
}
return zigZag(node.next, 0);
}
}
public static void Main(String[] args)
{
GFG lobj = new GFG();
lobj.head = new Node(11);
lobj.head.next = new Node(15);
lobj.head.next.next = new Node(20);
lobj.head.next.next.next = new Node(5);
lobj.head.next.next.next.next = new Node(10);
lobj.printLL();
int flag = 0;
lobj.zigZag(lobj.head, flag);
Console.WriteLine( "LL in zig zag fashion : " );
lobj.printLL();
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
var head;
function printLL() {
var t = head;
while (t != null ) {
document.write(t.data + " ->" );
t = t.next;
}
document.write();
}
function swap(a, b) {
if (a == null || b == null )
return ;
var temp = a.data;
a.data = b.data;
b.data = temp;
}
function zigZag(node , flag) {
if (node == null || node.next == null ) {
return node;
}
if (flag == 0) {
if (node.data > node.next.data) {
swap(node, node.next);
}
return zigZag(node.next, 1);
} else {
if (node.data < node.next.data) {
swap(node, node.next);
}
return zigZag(node.next, 0);
}
}
head = new Node(11);
head.next = new Node(15);
head.next.next = new Node(20);
head.next.next.next = new Node(5);
head.next.next.next.next = new Node(10);
printLL();
var flag = 0;
zigZag(head, flag);
document.write( "<br/>LL in zig zag fashion : <br/>" );
printLL();
</script>
|
Output11 ->15 ->20 ->5 ->10 ->
LL in zig zag fashion :
11 ->20 ->5 ->15 ->10 ->
Complexity Analysis:
- Time Complexity: O(n).
Traversal of the list is done only once, and it has ‘n’ elements. - Auxiliary Space: O(n).
O(n) extra space data structure for storing values.
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