Rearrange a Linked List in Zig-Zag fashion
Last Updated :
23 Jul, 2025
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++
// 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 of 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);
}
// This code is contributed by Sania Kumari Gupta (kriSania804)
C
// C program to arrange linked list in zigzag fashion
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
/* Link list Node */
typedef struct Node {
int data;
struct Node* next;
} Node;
// This function swaps values pointed by xp and yp
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// 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(¤t->data, ¤t->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(¤t->data, ¤t->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 = (Node*)malloc(sizeof(Node));
/* put in the data */
new_Node->data = new_data;
/* link the old list of 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);
}
// This code is contributed by Sania Kumari Gupta (kriSania804)
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 of 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.
Python
# Python code to rearrange linked list in zig zag fashion
# Node class
class Node:
# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None
# This function distributes the Node in zigzag fashion
def zigZagList(head):
# If flag is true, then next node should be greater
# in the desired output.
flag = True
# Traverse linked list starting from head.
current = head
while (current.next != None):
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):
t = current.data
current.data = current.next.data
current.next.data = t
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):
t = current.data
current.data = current.next.data
current.next.data = t
current = current.next
if(flag):
flag = False # flip flag for reverse checking
else:
flag = True
return head
# function to insert a Node in
# the linked list at the beginning.
def push(head, k):
tem = Node(0)
tem.data = k
tem.next = head
head = tem
return head
# function to display Node of linked list.
def display( head):
curr = head
while (curr != None):
print( curr.data, "->", end =" ")
curr = curr.next
print("None")
# Driver code
head = None
# create a list 4 -> 3 -> 7 -> 8 -> 6 -> 2 -> 1
# answer should be -> 3 7 4 8 2 6 1
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)
# This code is contributed by Arnab Kundu
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 of 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 */
Javascript
<script>
// Javascript program to arrange
// linked list in zigzag fashion
/* Link list Node */
class Node {
constructor() {
this.data = 0;
this.next = null;
}
}
var head = null;
var temp = 0;
// This function distributes
// the Node in zigzag fashion
function zigZagList(head) {
// If flag is true, then
// next node should be greater
// in the desired output.
var flag = true;
// Traverse linked list starting from head.
var 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 */
function push(new_data) {
/* allocate Node */
var new_Node = new Node();
/* put in the data */
new_Node.data = new_data;
/* link the old list of the new Node */
new_Node.next = (head);
/* move the head to point to the new Node */
(head) = new_Node;
}
/* Function to print linked list */
function printList(node) {
while (node != null) {
document.write(node.data + "->");
node = node.next;
}
document.write("NULL<br/>");
}
/* Driver code */
/* 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);
document.write("Given linked list <br/>");
printList(head);
zigZagList(head);
document.write("Zig Zag Linked list <br/>");
printList(head);
// This code contributed by gauravrajput1
</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++
// C++ program to arrange linked list
// in zigzag fashion
#include <iostream>
using namespace std;
// a linked list node
struct node {
int data;
node* next;
};
/* Function to push a 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;
}
// Rearrange the linked list in zig zag way
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);
}
}
// fun to print list
void printList(node* head)
{
while (head) {
cout << head->data << "-> ";
head = head->next;
}
cout << "NULL";
}
// main fun
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;
}
// This code is contributed by Upendra
C
// C program to arrange linked list in zigzag fashion
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
// a linked list node
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;
}
// function to swap the elements
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
// Rearrange the linked list in zig zag way
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);
}
}
// fun to print list
void printList(node* head)
{
while (head) {
printf("%d-> ", head->data);
head = head->next;
}
printf("NULL");
}
// main fun
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;
}
// This code is contributed by Aditya Kumar (adityakumar129)
Java
// Java program for the above approach
import java.io.*;
// Node class
class Node {
int data;
Node next;
Node(int data) { this.data = data; }
}
public class GFG {
private Node head;
// Print Linked List
public void printLL()
{
Node t = head;
while (t != null) {
System.out.print(t.data + " ->");
t = t.next;
}
System.out.println();
}
// Swap both nodes
public void swap(Node a, Node b)
{
if (a == null || b == null)
return;
int temp = a.data;
a.data = b.data;
b.data = temp;
}
// Rearrange the linked list
// in zig zag way
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);
}
}
// Driver Code
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();
// 0 means the current element
// should be smaller than the next
int flag = 0;
lobj.zigZag(lobj.head, flag);
System.out.println("LL in zig zag fashion : ");
lobj.printLL();
}
}
Python3
# Python program for the above approach// Node class
class Node:
# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None
head = None
# Print Linked List
def printLL():
t = head
while (t != None):
print(t.data, end=" ->")
t = t.next
print()
# Swap both nodes
def swap(a, b):
if(a == None or b == None):
return
temp = a.data
a.data = b.data
b.data = temp
# Rearrange the linked list
# in zig zag way
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)
# Driver Code
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()
# 0 means the current element
# should be smaller than the next
flag = 0
zigZag(head, flag)
print("LL in zig zag fashion : ")
printLL()
# This code is contributed by avanitrachhadiya2155.
C#
// C# program for the above approach
using System;
// Node class
public class Node {
public int data;
public Node next;
public
Node(int data)
{
this.data = data;
}
}
public class GFG {
private Node head;
// Print Linked List
public void printLL()
{
Node t = head;
while (t != null) {
Console.Write(t.data + " ->");
t = t.next;
}
Console.WriteLine();
}
// Swap both nodes
public void swap(Node a, Node b)
{
if (a == null || b == null)
return;
int temp = a.data;
a.data = b.data;
b.data = temp;
}
// Rearrange the linked list
// in zig zag way
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);
}
}
// Driver Code
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();
// 0 means the current element
// should be smaller than the next
int flag = 0;
lobj.zigZag(lobj.head, flag);
Console.WriteLine("LL in zig zag fashion : ");
lobj.printLL();
}
}
// This code is contributed by umadevi9616
Javascript
<script>
// javascript program for the above approach// Node class
class Node {
constructor(val) {
this.data = val;
this.next = null;
}
}
var head;
// Print Linked List
function printLL() {
var t = head;
while (t != null) {
document.write(t.data + " ->");
t = t.next;
}
document.write();
}
// Swap both nodes
function swap(a, b) {
if (a == null || b == null)
return;
var temp = a.data;
a.data = b.data;
b.data = temp;
}
// Rearrange the linked list
// in zig zag way
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);
}
}
// Driver Code
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();
// 0 means the current element
// should be smaller than the next
var flag = 0;
zigZag(head, flag);
document.write("<br/>LL in zig zag fashion : <br/>");
printLL();
// This code contributed by umadevi9616
</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.
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem