Traversal in Doubly Linked List
Last Updated :
19 Feb, 2025
Traversal of Doubly Linked List is one of the fundamental operations, where we traverse or visit each node of the linked list. In this article, we will cover how to traverse all the nodes of a doubly linked list and its implementation.
Examples:
Input: 10 <-> 20 <-> 30 <-> 40
Output: [10, 20, 30, 40]
and [40, 30, 20, 10]
Explanation:
- Forward traversal moves from the first node to the last:
[10, 20, 30, 40]
. - Backward traversal moves from the last node to the first:
[40, 30, 20, 10]
.
Input: 5 <-> 15 <-> 25 <-> 35 <-> 45 <-> 55
Output: [5, 15, 25, 35, 45, 55]
and [55, 45, 35, 25, 15, 5]
Explanation:
- Forward traversal follows the order of nodes:
[5, 15, 25, 35, 45, 55]
. - Backward traversal starts from the last node and moves backward:
[55, 45, 35, 25, 15, 5]
.
Input: 100 <-> 200 <-> 300
Output: [100, 200, 300]
and [300, 200, 100]
Explanation:
- Forward traversal reads nodes in order:
[100, 200, 300]
. - Backward traversal starts at the last node and moves to the first:
[300, 200, 100]
.
Types of Traversal in Doubly Linked List
Since each node of Doubly Linked List has pointer to the next node as well as the previous node, we can traverse the linked list in two directions:
- Forward Traversal
- Backward Traversal
Forward Traversal of Doubly Linked List
In Forward Traversal, we start from the first node, that is the head of the Doubly Linked List and continue visiting the next nodes using the next pointer of each node till we reach the last node of the linked list.
1. Iterative Approach for Forward Traversal
Follow the below steps:
- Initialize a pointer to the head of the linked list.
- While the pointer is not null:
- Visit the data at the current node.
- Move the pointer to the next node.
C++
// C++ Program for Forward Traversal (Iterative) of
// Doubly Linked List
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
Node *prev;
Node(int val) {
data = val;
next = nullptr;
prev = nullptr;
}
};
// Function to traverse the doubly linked list
// in forward direction
void forwardTraversal(Node *head) {
// Start traversal from the head of the list
Node *curr = head;
// Continue until current node is not null
// (end of list)
while (curr != nullptr) {
// Output data of the current node
cout << curr->data << " ";
// Move to the next node
curr = curr->next;
}
cout << endl;
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node *head = new Node(1);
Node *second = new Node(2);
Node *third = new Node(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
cout << "Forward Traversal: ";
forwardTraversal(head);
return 0;
}
C
// C Program for Forward Traversal (Iterative) of
// Doubly Linked List
#include <stdio.h>
// Definition of a Node in a doubly linked list
struct Node {
int data;
struct Node *next;
struct Node *prev;
};
// Function to traverse the doubly linked list
// in forward direction
void forwardTraversal(struct Node* head) {
struct Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
// Function to create a new node
struct Node* createNode(int val) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
struct Node* head = createNode(1);
struct Node* second = createNode(2);
struct Node* third = createNode(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
printf("Forward Traversal: ");
forwardTraversal(head);
return 0;
}
Java
// Java Program for Forward Traversal (Iterative) of
// Doubly Linked List
class Node {
int data;
Node next;
Node prev;
Node(int val) {
data = val;
next = null;
prev = null;
}
}
class GFG {
// Function to traverse the doubly linked list in
// forward direction
static void forwardTraversal(Node head) {
Node curr = head;
while (curr != null) {
// Output data of the current node
System.out.print(curr.data + " ");
// Move to the next node
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
System.out.print("Forward Traversal: ");
forwardTraversal(head);
}
}
Python
# Python Program for Forward Traversal (Iterative) of
# Doubly Linked List
class Node:
def __init__(self, val):
self.data = val
self.next = None
self.prev = None
# Function to traverse the doubly linked list
# in forward direction
def forward_traversal(head):
curr = head
while curr is not None:
# Output data of the current node
print(curr.data, end=" ")
# Move to the next node
curr = curr.next
print()
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3
head = Node(1)
second = Node(2)
third = Node(3)
head.next = second
second.prev = head
second.next = third
third.prev = second
print("Forward Traversal: ", end="")
forward_traversal(head)
C#
// C# Program for Forward Traversal (Iterative) of
// Doubly Linked List
using System;
class Node {
public int Data;
public Node Next;
public Node Prev;
public Node(int data) {
Data = data;
Next = null;
Prev = null;
}
}
class GfG {
// Function to traverse the doubly linked list
// in forward direction
static void ForwardTraversal(Node head) {
Node curr = head;
while (curr != null) {
// Output data of the current node
Console.Write(curr.Data + " ");
// Move to the next node
curr = curr.Next;
}
Console.WriteLine();
}
static void Main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.Next = second;
second.Prev = head;
second.Next = third;
third.Prev = second;
Console.Write("Forward Traversal: ");
ForwardTraversal(head);
}
}
JavaScript
// Javascript Program for Forward Traversal (Iterative) of
// Doubly Linked List
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
// Function to traverse the doubly linked list in forward direction
function forwardTraversal(head) {
let curr = head;
while (curr !== null) {
// Output data of the current node
console.log(curr.data + " ");
// Move to the next node
curr = curr.next;
}
console.log();
}
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
let head = new Node(1);
let second = new Node(2);
let third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
console.log("Forward Traversal: ");
forwardTraversal(head);
OutputForward Traversal: 1 2 3
Time Complexity: O(n), where n is the number of nodes in the linked list
Auxiliary Space: O(1)
2. Recursive Approach for Forward Traversal
Follow the below steps:
- Maintain a recursive function, say forwardTraversal(head) which takes the pointer to a node as parameter.
- Inside forwardTraversal(head)
- If the head pointer is NULL, then simply return from the function.
- Otherwise, print the data inside the node and call the recursive function with the next node, forwardTraversal(head->next).
C++
// C++ Program for Forward Traversal (Recursive) of
// Doubly Linked List
#include <iostream>
using namespace std;
struct Node {
int data;
Node *next;
Node *prev;
Node(int val) {
data = val;
prev = next = nullptr;
}
};
// Recursive function for forward traversal
void forwardTraversal(Node *head) {
if (head == nullptr)
return;
// Print current node's data
cout << head->data << " ";
// Recursive call with the next node
forwardTraversal(head->next);
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node *head = new Node(1);
Node *second = new Node(2);
Node *third = new Node(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
cout << "Forward Traversal: ";
forwardTraversal(head);
return 0;
}
C
// C Program for Forward Traversal (Recursive) of
// Doubly Linked List
#include <stdio.h>
struct Node {
int data;
struct Node *next;
struct Node *prev;
};
// Recursive function for forward traversal
void forwardTraversal(struct Node *head) {
if (head == NULL)
return;
// Print current node's data
printf("%d ", head->data);
// Recursive call with the next node
forwardTraversal(head->next);
}
// Function to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
int main() {
// Create a hardcoded doubly linked list: 1 <-> 2 <-> 3
struct Node *head = createNode(1);
struct Node *second = createNode(2);
struct Node *third = createNode(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
printf("Forward Traversal: ");
forwardTraversal(head);
return 0;
}
Java
// Java Program for Forward Traversal (Recursive) of
// Doubly Linked List
class Node {
int data;
Node next;
Node prev;
// Constructor to initialize the node
Node(int val) {
data = val;
next = null;
prev = null;
}
}
class GfG {
// Recursive function for forward traversal
static void forwardTraversal(Node head) {
if (head == null) {
return;
}
// Print current node's data
System.out.print(head.data + " ");
// Recursive call with the next node
forwardTraversal(head.next);
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
System.out.print("Forward Traversal: ");
forwardTraversal(head);
}
}
Python
# Python Program for Forward Traversal (Recursive) of
# Doubly Linked List
class Node:
def __init__(self, val):
self.data = val
self.next = None
self.prev = None
# Recursive function for forward traversal
def forward_traversal(head):
if head is None:
return
# Print current node's data
print(head.data, end=" ")
# Recursive call with the next node
forward_traversal(head.next)
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3
head = Node(1)
second = Node(2)
third = Node(3)
head.next = second
second.prev = head
second.next = third
third.prev = second
print("Forward Traversal:", end=" ")
forward_traversal(head)
C#
// C# Program for Forward Traversal (Recursive) of
// Doubly Linked List
using System;
class Node {
public int Data;
public Node Next;
public Node Prev;
public Node(int val) {
Data = val;
Next = null;
Prev = null;
}
}
class GfG {
// Recursive function for forward traversal
static void ForwardTraversal(Node head) {
if (head == null)
return;
// Print current node's data
Console.Write(head.Data + " ");
// Recursive call with the next node
ForwardTraversal(head.Next);
}
static void Main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.Next = second;
second.Prev = head;
second.Next = third;
third.Prev = second;
Console.Write("Forward Traversal: ");
ForwardTraversal(head);
}
}
JavaScript
// JavaScript Program for Forward Traversal (Recursive) of
// Doubly Linked List
class Node {
constructor(val) {
this.data = val;
this.next = null;
this.prev = null;
}
}
// Recursive function for forward traversal
function forwardTraversal(node) {
if (node === null) return;
// Print current node's data
console.log(node.data + " ");
// Recursive call with the next node
forwardTraversal(node.next);
}
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
let head = new Node(1);
let second = new Node(2);
let third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
console.log("Forward Traversal: ");
forwardTraversal(head);
OutputForward Traversal: 1 2 3
Time Complexity: O(n), where n is the number of nodes in the linked list
Auxiliary Space: O(n)
Backward Traversal of Doubly Linked List
In Backward Traversal, we start from the last node, that is the tail of the Doubly Linked List and continue visiting the previous nodes using the prev pointer of each node till we reach the first node of the linked list.
1. Iterative Approach for Backward Traversal
Follow the below steps:
- Initialize a pointer to the tail of the linked list.
- While the pointer is not null:
- Visit the data at the current node.
- Move the pointer to the previous node.
C++
// C++ Program for Backward Traversal (Iterative) of
// Doubly Linked List
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
Node(int val) {
data = val;
next = prev = nullptr;
}
};
// Function to traverse the doubly linked list
// in backward direction
void backwardTraversal(Node* tail) {
// Start traversal from the tail of the list
Node* curr = tail;
// Continue until current node is not null
// (start of list)
while (curr != nullptr) {
// Output data of the current node
cout << curr->data << " ";
// Move to the previous node
curr = curr->prev;
}
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node* head = new Node(1);
Node* second = new Node(2);
Node* third = new Node(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
cout << "Backward Traversal: ";
backwardTraversal(third);
return 0;
}
C
// C Program for Backward Traversal (Iterative) of
// Doubly Linked List
#include <stdio.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// Function to create a new node
struct Node* createNode(int val) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
// Function to traverse the doubly linked list
// in backward direction
void backwardTraversal(struct Node* tail) {
// Start traversal from the tail of the list
struct Node* curr = tail;
// Continue until current node is not null
while (curr != NULL) {
// Output data of the current node
printf("%d ", curr->data);
// Move to the previous node
curr = curr->prev;
}
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
struct Node* head = createNode(1);
struct Node* second = createNode(2);
struct Node* third = createNode(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
printf("Backward Traversal: ");
backwardTraversal(third);
// Free the allocated memory
free(head);
free(second);
free(third);
return 0;
}
Java
// Java Program for Backward Traversal (Iterative) of
// Doubly Linked List
class Node {
int data;
Node next;
Node prev;
Node(int val) {
data = val;
next = null;
prev = null;
}
}
class GfG {
// Function to traverse the doubly linked list in
// backward direction
public static void backwardTraversal(Node tail) {
// Start traversal from the tail of the list
Node curr = tail;
// Continue until current node is not null
while (curr != null) {
// Output data of the current node
System.out.print(curr.data + " ");
// Move to the previous node
curr = curr.prev;
}
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
System.out.print("Backward Traversal: ");
backwardTraversal(third);
}
}
Python
# Python Program for Backward Traversal (Iterative) of
# Doubly Linked List
class Node:
def __init__(self, val):
self.data = val
self.next = None
self.prev = None
def backward_traversal(tail):
curr = tail
# Traverse the list in backward direction
while curr is not None:
# Output data of the current node
print(curr.data, end=" ")
# Move to the previous node
curr = curr.prev
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3
head = Node(1)
second = Node(2)
third = Node(3)
head.next = second
second.prev = head
second.next = third
third.prev = second
print("Backward Traversal: ", end="")
backward_traversal(third)
C#
// C# Program for Backward Traversal (Iterative) of
// Doubly Linked List
using System;
public class Node {
public int Data;
public Node Next;
public Node Prev;
public Node(int val) {
Data = val;
Next = Prev = null;
}
}
class GfG {
// Function to traverse the doubly linked list in backward direction
static void BackwardTraversal(Node tail) {
Node curr = tail;
// Continue until current node is not null (start of list)
while (curr != null) {
// Output data of the current node
Console.Write(curr.Data + " ");
// Move to the previous node
curr = curr.Prev;
}
}
static void Main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.Next = second;
second.Prev = head;
second.Next = third;
third.Prev = second;
Console.Write("Backward Traversal: ");
BackwardTraversal(third);
}
}
JavaScript
// JavaScript Program for Backward Traversal (Iterative) of
// Doubly Linked List
class Node {
constructor(val) {
this.data = val;
this.next = null;
this.prev = null;
}
}
// Function to traverse the doubly linked list in backward direction
function backwardTraversal(tail) {
let curr = tail;
// Continue until current node is not null (start of list)
while (curr !== null) {
// Output data of the current node
console.log(curr.data + " ");
// Move to the previous node
curr = curr.prev;
}
}
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
const head = new Node(1);
const second = new Node(2);
const third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
console.log("Backward Traversal: ");
backwardTraversal(third);
OutputBackward Traversal: 3 2 1
Time Complexity: O(n), where n is the number of nodes in the linked list
Auxiliary Space: O(1)
2. Recursive Approach for Backward Traversal
Follow the below steps:
- Maintain a recursive function, say backwardTraversal(node) which takes the pointer to a node as parameter.
- Inside backwardTraversal(node)
- If the head pointer is NULL, then simply return from the function.
- Otherwise, print the data inside the node and call the recursive function with the previous node, backwardTraversal(node->prev).
C++
// C++ Program for Backward Traversal (Recursive) of
// Doubly Linked List
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
Node(int val) {
data = val;
next = prev = nullptr;
}
};
// Recursive function for backward traversal
void backwardTraversal(Node* node) {
if (node == nullptr) return;
// Print current node's data
cout << node->data << " ";
// Recursive call with the previous node
backwardTraversal(node->prev);
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node* head = new Node(1);
Node* second = new Node(2);
Node* third = new Node(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
cout << "Backward Traversal: ";
backwardTraversal(third);
cout << endl;
return 0;
}
C
// C Program for Backward Traversal (Recursive) of
// Doubly Linked List
#include <stdio.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// Function to create a new node
struct Node* createNode(int val) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = val;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
// Recursive function for backward traversal
void backwardTraversal(struct Node* node) {
if (node == NULL) return;
// Print current node's data
printf("%d ", node->data);
// Recursive call with the previous node
backwardTraversal(node->prev);
}
int main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
struct Node* head = createNode(1);
struct Node* second = createNode(2);
struct Node* third = createNode(3);
head->next = second;
second->prev = head;
second->next = third;
third->prev = second;
printf("Backward Traversal: ");
backwardTraversal(third);
return 0;
}
Java
// Java Program for Backward Traversal (Recursive) of
// Doubly Linked List
class Node {
int data;
Node next;
Node prev;
Node(int val) {
data = val;
next = null;
prev = null;
}
}
public class GfG {
// Recursive function for backward traversal
static void backwardTraversal(Node node) {
if (node == null)
return;
// Print current node's data
System.out.print(node.data + " ");
// Recursive call with the previous node
backwardTraversal(node.prev);
}
public static void main(String[] args) {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
System.out.print("Backward Traversal: ");
backwardTraversal(third);
System.out.println();
}
}
Python
# Python Program for Backward Traversal (Recursive) of
# Doubly Linked List
class Node:
def __init__(self, val):
self.data = val
self.next = None
self.prev = None
def backward_traversal(node):
if node is None:
return
# Print current node's data
print(node.data, end=" ")
# Recursive call with the previous node
backward_traversal(node.prev)
if __name__ == "__main__":
# Create a hardcoded doubly linked list:
# 1 <-> 2 <-> 3
head = Node(1)
second = Node(2)
third = Node(3)
head.next = second
second.prev = head
second.next = third
third.prev = second
print("Backward Traversal: ", end="")
backward_traversal(third)
C#
// C# Program for Backward Traversal (Recursive) of
// Doubly Linked List
using System;
class Node
{
public int Data;
public Node Next;
public Node Prev;
public Node(int val) {
Data = val;
Next = null;
Prev = null;
}
}
class GfG {
static void BackwardTraversal(Node node) {
if (node == null)
return;
// Print current node's data
Console.Write(node.Data + " ");
// Recursive call with the previous node
BackwardTraversal(node.Prev);
}
static void Main() {
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
Node head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
head.Next = second;
second.Prev = head;
second.Next = third;
third.Prev = second;
Console.Write("Backward Traversal: ");
BackwardTraversal(third);
Console.WriteLine();
}
}
JavaScript
// Javascript Program for Backward Traversal (Recursive) of
// Doubly Linked List
class Node {
constructor(val) {
this.data = val;
this.next = null;
this.prev = null;
}
}
function backwardTraversal(node) {
if (node === null) return;
// Print current node's data
console.log(node.data + " ");
// Recursive call with the previous node
backwardTraversal(node.prev);
}
// Create a hardcoded doubly linked list:
// 1 <-> 2 <-> 3
const head = new Node(1);
const second = new Node(2);
const third = new Node(3);
head.next = second;
second.prev = head;
second.next = third;
third.prev = second;
console.log("Backward Traversal: ");
backwardTraversal(third);
OutputBackward Traversal: 3 2 1
Time Complexity: O(n), where n is the number of nodes in the linked list
Auxiliary Space: O(n)
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