Rearrange a Linked List in Zig-Zag fashion | Set-2
Last Updated :
27 Apr, 2023
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:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Node* zigZagList(Node* head)
{
if (head == NULL || head->next == NULL) {
return head;
}
Node* res = NULL;
Node* curr = head;
Node* prev = NULL;
Node* next;
int ind = 0;
while (curr->next) {
if ((ind == 0 && curr->data > curr->next->data)
|| (ind == 1 && curr->data < curr->next->data)) {
if (res == NULL)
res = curr->next;
next = curr->next->next;
if (prev)
prev->next = curr->next;
curr->next->next = curr;
curr->next = next;
if (prev)
prev = prev->next;
else
prev = res;
}
else {
if (res == NULL) {
res = curr;
}
prev = curr;
curr = curr->next;
}
ind = 1 - ind;
}
return res;
}
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;
}
}
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);
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;
static Node zigZagList(Node head)
{
if (head == null || head.next == null )
{
return head;
}
Node res = null ;
Node curr = head;
Node prev = null ;
Node next;
int ind = 0 ;
while (curr.next != null )
{
if ((ind == 0 && curr.data > curr.next.data) ||
(ind == 1 && curr.data < curr.next.data))
{
if (res == null )
res = curr.next;
next = curr.next.next;
if (prev != null )
prev.next = curr.next;
curr.next.next = curr;
curr.next = next;
if (prev != null )
prev = prev.next;
else
prev = res;
}
else
{
if (res == null )
{
res = curr;
}
prev = curr;
curr = curr.next;
}
ind = 1 - ind;
}
return res;
}
static void push(Node head_ref, int new_data)
{
Node new_Node = new Node();
new_Node.data = new_data;
new_Node.next = head_ref;
head_ref = new_Node;
head = head_ref;
}
static void printList(Node Node)
{
while (Node != null )
{
System.out.printf( "%d->" , Node.data);
Node = Node.next;
}
}
public static void main(String[] args)
{
head = null ;
push(head, 1 );
push(head, 2 );
push(head, 6 );
push(head, 8 );
push(head, 7 );
push(head, 3 );
push(head, 4 );
System.out.printf( "Given linked list \n" );
printList(head);
head = zigZagList(head);
System.out.printf( "\nZig Zag Linked list \n" );
printList(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def zigZagList(head):
if head = = None or head. next = = None :
return head
res = None
curr = head
prev = None
ind = 0
while curr. next :
if ((ind = = 0 and curr.data > curr. next .data)
or (ind = = 1 and curr.data < curr. next .data)):
if res = = None :
res = curr. next
next = curr. next . next
if prev:
prev. next = curr. next
curr. next . next = curr
curr. next = next
if prev:
prev = prev. next
else :
prev = res
else :
if res = = None :
res = curr
prev = curr
curr = curr. next
ind = 1 - ind
return res
def push(head_ref, new_data):
new_Node = Node(new_data)
new_Node. next = head_ref
head_ref = new_Node
return head_ref
def printList(Node):
while Node ! = None :
print (Node.data, end = "->" )
Node = Node. next
if __name__ = = "__main__" :
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" )
printList(head)
head = zigZagList(head)
print ( "\nZig Zag Linked list" )
printList(head)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node head;
static Node zigZagList(Node head)
{
if (head == null || head.next == null )
{
return head;
}
Node res = null ;
Node curr = head;
Node prev = null ;
Node next;
int ind = 0;
while (curr.next != null )
{
if ((ind == 0 && curr.data > curr.next.data) ||
(ind == 1 && curr.data < curr.next.data))
{
if (res == null )
res = curr.next;
next = curr.next.next;
if (prev != null )
prev.next = curr.next;
curr.next.next = curr;
curr.next = next;
if (prev != null )
prev = prev.next;
else
prev = res;
}
else
{
if (res == null )
{
res = curr;
}
prev = curr;
curr = curr.next;
}
ind = 1 - ind;
}
return res;
}
static void push(Node head_ref, int new_data)
{
Node new_Node = new Node();
new_Node.data = new_data;
new_Node.next = head_ref;
head_ref = new_Node;
head = head_ref;
}
static void printList(Node Node)
{
while (Node != null )
{
Console.Write( "{0}->" , Node.data);
Node = Node.next;
}
}
public static void Main(String[] args)
{
head = null ;
push(head, 1);
push(head, 2);
push(head, 6);
push(head, 8);
push(head, 7);
push(head, 3);
push(head, 4);
Console.Write( "Given linked list \n" );
printList(head);
head = zigZagList(head);
Console.Write( "\nZig Zag Linked list \n" );
printList(head);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
}
var head = null ;
function zigZagList(head)
{
if (head == null || head.next == null )
{
return head;
}
var res = null ;
var curr = head;
var prev = null ;
var next;
var ind = 0;
while (curr.next != null )
{
if ((ind == 0 && curr.data > curr.next.data) ||
(ind == 1 && curr.data < curr.next.data))
{
if (res == null ) res = curr.next;
next = curr.next.next;
if (prev != null ) prev.next = curr.next;
curr.next.next = curr;
curr.next = next;
if (prev != null ) prev = prev.next;
else prev = res;
}
else
{
if (res == null )
{
res = curr;
}
prev = curr;
curr = curr.next;
}
ind = 1 - ind;
}
return res;
}
function push(head_ref, new_data)
{
var new_Node = new Node();
new_Node.data = new_data;
new_Node.next = head_ref;
head_ref = new_Node;
head = head_ref;
}
function printList(Node)
{
while (Node != null )
{
document.write(Node.data + "->" );
Node = Node.next;
}
}
head = null ;
push(head, 1);
push(head, 2);
push(head, 6);
push(head, 8);
push(head, 7);
push(head, 3);
push(head, 4);
document.write( "Given linked list <br>" );
printList(head);
head = zigZagList(head);
document.write( "<br>Zig Zag Linked list <br>" );
printList(head);
</script>
|
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...