Java Program For Rearranging A Linked List In Zig-Zag Fashion
Last Updated :
22 Aug, 2024
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.
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);
}
}
|
Output:
Given linked list
4->3->7->8->6->2->1->NULL
Zig Zag Linked list
3->7->4->8->2->6->1->NULL
Time Complexity: O(n),The time complexity of this algorithm is O(n) as it only requires one iteration through the linked list.
Space Complexity: O(1),The space complexity is also O(1) as we only need a few variables to store data.
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.
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();
}
}
|
Output:
11 ->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 due to recursive stack.
Please refer complete article on Rearrange a Linked List in Zig-Zag fashion for more details!
Similar Reads
Java Program For Rearranging A Given Linked List In-Place.
Given a singly linked list L0 -> L1 -> ⦠-> Ln-1 -> Ln. Rearrange the nodes in the list so that the new formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 ...You are required to do this in place without altering the nodes' values. Examples: Input: 1 -> 2 -> 3 -
8 min read
Java Program For Reversing A Doubly Linked List
Given a Doubly Linked List, the task is to reverse the given Doubly Linked List. See below diagrams for example. (a) Original Doubly Linked List (b) Reversed Doubly Linked List Here is a simple method for reversing a Doubly Linked List. All we need to do is swap prev and next pointers for all nodes,
5 min read
Java Program For Rotating A Linked List
Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40. Assume that k is smal
6 min read
Java Program For Reversing Alternate K Nodes In A Singly Linked List
Given a linked list, write a function to reverse every alternate k nodes (where k is an input to the function) in an efficient way. Give the complexity of your algorithm. Example: Inputs: 1->2->3->4->5->6->7->8->9->NULL and k = 3 Output: 3->2->1->4->5->6-
6 min read
Java Program For Swapping Nodes In A Linked List Without Swapping Data
Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct. Examples: Input : 10->15-
6 min read
Java Program For Reversing A Linked List In Groups Of Given Size- Set 2
Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Examples: Input: 1->2->3->4->5->6->7->8->NULL and k = 3 Output: 3->2->1->6->5->4->8->7->NULL. Input: 1->2->3->4->5->6->7->8->N
3 min read
Java Program For Segregating Even And Odd Nodes In A Linked List
Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers same.Examples: Input: 17->15->8->12->10->5->4->1->7->6->NUL
6 min read
Java Program for Reverse a linked list
Given a pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input: Head of following linked list 1->2->3->4->NULLOutput: Linked list should be changed to, 4->3->2->1->NULL In
3 min read
Java Program For Flattening A Linked List
Given a linked list where every node represents a linked list and contains two pointers of its type: Pointer to next node in the main list (we call it 'right' pointer in the code below).Pointer to a linked list where this node is headed (we call it the 'down' pointer in the code below). All linked l
4 min read
Java Program For Reversing A Linked List In Groups Of Given Size - Set 1
Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Example: Input: 1->2->3->4->5->6->7->8->NULL, K = 3 Output: 3->2->1->6->5->4->8->7->NULL Input: 1->2->3->4->5->6->7->8->NULL, K = 5 Output: 5->4->3->2->1->8->7->6->NULL Recommended: Please solve
3 min read