Deletion from a Circular Linked List
Last Updated :
19 Dec, 2024
In this article, we will learn how to delete a node from a circular linked list. In a circular linked list, the last node connects back to the first node, creating a loop.
There are three main ways to delete a node from circular linked list:
- Deletion at the beginning
- Deletion at specific position
- Deletion at the end
Now, let’s look at the methods and steps for these three deletion operations.
Deletion from a Circular Linked List:
Deletion involves removing a node from the linked list. The main difference is that we need to ensure the list remains circular after the deletion. We can delete a node in a circular linked list in three ways:
1. Deletion from the beginning of the circular linked list
To delete the first node of a circular linked list, we first check if the list is empty. If it is then we print a message and return NULL. If the list contains only one node (the head is the same as the last) then we delete that node and set the last pointer to NULL. If there are multiple nodes then we update the last->next pointer to skip the head node and effectively removing it from the list. We then delete the head node to free the allocated memory. Finally, we return the updated last pointer, which still points to the last node in the list.
Delete the first node in circular linked listStep-by-step approach:
- Check if List is Empty:
- If last is nullptr, print "List is empty" and return nullptr.
- Get Head Node:
- Check for Single Node:
- If head equals last, delete head and set last to nullptr.
- Handle Multiple Nodes:
- Update last->next to point to head->next.
- Delete head.
- Return Updated last
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
// Function to delete the first node of the circular linked list
Node* deleteFirstNode(Node* last) {
if (last == nullptr) {
// If the list is empty
cout << "List is empty" << endl;
return nullptr;
}
Node* head = last->next;
if (head == last) {
// If there is only one node in the list
delete head;
last = nullptr;
} else {
// More than one node in the list
last->next = head->next;
delete head;
}
return last;
}
void printList(Node* last) {
if(last == NULL) return ;
Node *head = last->next;
while (true){
cout << head->data << " ";
head = head->next;
if (head == last->next) break;
}
cout << endl;
}
int main() {
// Create circular linked list: 2, 3, 4
Node* first = new Node(2);
first->next = new Node(3);
first->next->next = new Node(4);
Node* last = first->next->next;
last->next = first;
cout << "Original list: ";
printList(last);
// Delete the first node
last = deleteFirstNode(last);
cout << "List after deleting first node: ";
printList(last);
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* deleteFirstNode(struct Node* last) {
if (last == NULL) {
// If the list is empty
printf("List is empty\n");
return NULL;
}
struct Node* head = last->next;
if (head == last) {
// If there is only one node in the list
free(head);
last = NULL;
} else {
// More than one node in the list
last->next = head->next;
free(head);
}
return last;
}
void printList(struct Node* last) {
if (last == NULL) return;
struct Node* head = last->next;
while (1) {
printf("%d ", head->data);
head = head->next;
if (head == last->next) break;
}
printf("\n");
}
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
int main() {
struct Node* first = createNode(2);
first->next = createNode(3);
first->next->next = createNode(4);
struct Node* last = first->next->next;
last->next = first;
printf("Original list: ");
printList(last);
last = deleteFirstNode(last);
printf("List after deleting first node: ");
printList(last);
return 0;
}
Java
class Node {
int data;
Node next;
Node(int value) {
data = value;
next = null;
}
}
public class GFG {
public static Node deleteFirstNode(Node last) {
if (last == null) {
// If the list is empty
System.out.println("List is empty");
return null;
}
Node head = last.next;
if (head == last) {
// If there is only one node in the list
last = null;
} else {
// More than one node in the list
last.next = head.next;
}
return last;
}
public static void printList(Node last) {
if (last == null) return;
Node head = last.next;
while (true) {
System.out.print(head.data + " ");
head = head.next;
if (head == last.next) break;
}
System.out.println();
}
public static void main(String[] args) {
// Create circular linked list: 2, 3, 4
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
System.out.print("Original list: ");
printList(last);
// Delete the first node
last = deleteFirstNode(last);
System.out.print("List after deleting first node: ");
printList(last);
}
}
Python
class Node:
def __init__(self, data):
self.data = data
self.next = None
def deleteFirstNode(last):
if last is None:
# If the list is empty
print("List is empty")
return None
head = last.next
if head == last:
# If there is only one node in the list
last = None
else:
# More than one node in the list
last.next = head.next
return last
def print_list(last):
if last is None:
return
head = last.next
while True:
print(head.data, end=" ")
head = head.next
if head == last.next:
break
print()
# Create circular linked list: 2, 3, 4
first = Node(2)
first.next = Node(3)
first.next.next = Node(4)
last = first.next.next
last.next = first
print("Original list: ", end="")
print_list(last)
# Delete the first node
last = deleteFirstNode(last)
print("List after deleting first node: ", end="")
print_list(last)
JavaScript
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
function deleteFirstNode(last) {
if (last === null) {
// If the list is empty
console.log("List is empty");
return null;
}
let head = last.next;
if (head === last) {
// If there is only one node in the list
last = null;
} else {
// More than one node in the list
last.next = head.next;
}
return last;
}
function printList(last) {
if (last === null) return;
let head = last.next;
while (true) {
console.log(head.data + " ");
head = head.next;
if (head === last.next) break;
}
console.log();
}
// Create circular linked list: 2, 3, 4
let first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
let last = first.next.next;
last.next = first;
console.log("Original list: ");
printList(last);
// Delete the first node
last = deleteFirstNode(last);
console.log("List after deleting first node: ");
printList(last);
OutputOriginal list: 2 3 4
List after deleting first node: 3 4
Time Complexity: O(1)
Auxiliary Space: O(1)
2. Deletion at specific position in circular linked list
To delete a specific node from a circular linked list, we first check if the list is empty. If it is then we print a message and return nullptr. If the list contains only one node and it matches the key then we delete that node and set last to nullptr. If the node to be deleted is the first node then we update the next pointer of the last node to skip the head node and delete the head. For other nodes, we traverse the list using two pointers: curr (to find the node) and prev (to keep track of the previous node). If we find the node with the matching key then we update the next pointer of prev to skip the curr node and delete it. If the node is found and it is the last node, we update the last pointer accordingly. If the node is not found then do nothing and tail or last as it is. Finally, we return the updated last pointer.
Delete a specific node in circular linked listStep-by-step approach:
- Check if List is Empty (last is nullptr) then print "List is empty, nothing to delete" and return nullptr.
- Set curr to last->next (head) and prev to last.
- Check for single node, if node's data matches key, delete it and set last to nullptr.
- Check for first node to delete, if head node's data matches key, update last->next and delete the head.
- Loop through the list to find the node with the specified key.
- If node found then update prev->next to skip the deleted node. If the deleted node is last, update last to prev.
- Otherwise, print "Node with data [key] not found."
- Return the modified last.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
// Function to delete a specific node in the circular linked list
Node* deleteSpecificNode(Node* last, int key) {
if (last == nullptr) {
// If the list is empty
cout << "List is empty, nothing to delete." << endl;
return nullptr;
}
Node* curr = last->next;
Node* prev = last;
// If the node to be deleted is the only node in the list
if (curr == last && curr->data == key) {
delete curr;
last = nullptr;
return last;
}
// If the node to be deleted is the first node
if (curr->data == key) {
last->next = curr->next;
delete curr;
return last;
}
// Traverse the list to find the node to be deleted
while (curr != last && curr->data != key) {
prev = curr;
curr = curr->next;
}
// If the node to be deleted is found
if (curr->data == key) {
prev->next = curr->next;
if (curr == last) {
last = prev;
}
delete curr;
} else {
// If the node to be deleted is not found
cout << "Node with data " << key
<< " not found." << endl;
}
return last;
}
// Function to print the circular linked list
void printList(Node* last) {
if (last == NULL){
cout << "List is Empty";
return;
}
Node *head = last->next;
while (true){
cout << head->data << " ";
head = head->next;
if (head == last->next) break;
}
cout << endl;
}
int main() {
// Create circular linked list: 2, 3, 4
Node* first = new Node(2);
first->next = new Node(3);
first->next->next = new Node(4);
Node* last = first->next->next;
last->next = first;
cout << "Original list: ";
printList(last);
// Delete a specific node
int key = 3;
last = deleteSpecificNode(last, key);
cout << "List after deleting node " << key << ": ";
printList(last);
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the circular linked list
struct Node {
int data;
struct Node* next;
};
// Function to delete a specific node in the circular linked list
struct Node* deleteSpecificNode(struct Node* last, int key) {
if (last == NULL) {
// If the list is empty
printf("List is empty, nothing to delete.\n");
return NULL;
}
struct Node* curr = last->next;
struct Node* prev = last;
// If the node to be deleted is the only node in the list
if (curr == last && curr->data == key) {
free(curr);
last = NULL;
return last;
}
// If the node to be deleted is the first node
if (curr->data == key) {
last->next = curr->next;
free(curr);
return last;
}
// Traverse the list to find the node to be deleted
while (curr != last && curr->data != key) {
prev = curr;
curr = curr->next;
}
// If the node to be deleted is found
if (curr->data == key) {
prev->next = curr->next;
if (curr == last) {
last = prev;
}
free(curr);
} else {
// If the node to be deleted is not found
printf("Node with data %d not found.\n", key);
}
return last;
}
void printList(struct Node* last) {
if (last == NULL) {
printf("List is Empty");
return;
}
struct Node* head = last->next;
while (1) {
printf("%d ", head->data);
head = head->next;
if (head == last->next) break;
}
printf("\n");
}
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
int main() {
// Create circular linked list: 2, 3, 4
struct Node* first = createNode(2);
first->next = createNode(3);
first->next->next = createNode(4);
struct Node* last = first->next->next;
last->next = first;
printf("Original list: ");
printList(last);
// Delete a specific node
int key = 3;
last = deleteSpecificNode(last, key);
printf("List after deleting node %d: ", key);
printList(last);
return 0;
}
Java
class Node {
int data;
Node next;
Node(int value){
data = value;
next = null;
}
}
public class GFG {
public static Node deleteSpecificNode(Node last,
int key){
if (last == null) {
// If the list is empty
System.out.println(
"List is empty, nothing to delete.");
return null;
}
Node curr = last.next;
Node prev = last;
// If the node to be deleted is the only node in the
// list
if (curr == last && curr.data == key) {
last = null;
return last;
}
// If the node to be deleted is the first node
if (curr.data == key) {
last.next = curr.next;
return last;
}
// Traverse the list to find the node to be deleted
while (curr != last && curr.data != key) {
prev = curr;
curr = curr.next;
}
// If the node to be deleted is found
if (curr.data == key) {
prev.next = curr.next;
if (curr == last) {
last = prev;
}
}
else {
// If the node to be deleted is not found
System.out.println("Node with data " + key
+ " not found.");
}
return last;
}
public static void printList(Node last){
if (last == null) {
System.out.println("List is Empty");
return;
}
Node head = last.next;
while (true) {
System.out.print(head.data + " ");
head = head.next;
if (head == last.next)
break;
}
System.out.println();
}
public static void main(String[] args){
// Create circular linked list: 2, 3, 4
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
System.out.print("Original list: ");
printList(last);
// Delete a specific node
int key = 3;
last = deleteSpecificNode(last, key);
System.out.print("List after deleting node " + key
+ ": ");
printList(last);
}
}
Python
class Node:
def __init__(self, data):
self.data = data
self.next = None
def deleteSpecificNode(last, key):
if last is None:
# If the list is empty
print("List is empty, nothing to delete.")
return None
curr = last.next
prev = last
# If the node to be deleted is the only node in the list
if curr == last and curr.data == key:
last = None
return last
# If the node to be deleted is the first node
if curr.data == key:
last.next = curr.next
return last
# Traverse the list to find the node to be deleted
while curr != last and curr.data != key:
prev = curr
curr = curr.next
# If the node to be deleted is found
if curr.data == key:
prev.next = curr.next
if curr == last:
last = prev
else:
# If the node to be deleted is not found
print(f"Node with data {key} not found.")
return last
def printList(last):
if last is None:
print("List is Empty")
return
head = last.next
while True:
print(head.data, end=" ")
head = head.next
if head == last.next:
break
print()
# Create circular linked list: 2, 3, 4
first = Node(2)
first.next = Node(3)
first.next.next = Node(4)
last = first.next.next
last.next = first
print("Original list: ", end="")
printList(last)
# Delete a specific node
key = 3
last = deleteSpecificNode(last, key)
print(f"List after deleting node {key}: ", end="")
printList(last)
JavaScript
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
function deleteSpecificNode(last, key) {
if (last === null) {
// If the list is empty
console.log("List is empty, nothing to delete.");
return null;
}
let curr = last.next;
let prev = last;
// If the node to be deleted is the only node in the list
if (curr === last && curr.data === key) {
last = null;
return last;
}
// If the node to be deleted is the first node
if (curr.data === key) {
last.next = curr.next;
return last;
}
// Traverse the list to find the node to be deleted
while (curr !== last && curr.data !== key) {
prev = curr;
curr = curr.next;
}
// If the node to be deleted is found
if (curr.data === key) {
prev.next = curr.next;
if (curr === last) {
last = prev;
}
} else {
// If the node to be deleted is not found
console.log("Node with data " + key + " not found.");
}
return last;
}
function printList(last) {
if (last === null) {
console.log("List is Empty");
return;
}
let head = last.next;
while (true) {
console.log(head.data + " ");
head = head.next;
if (head === last.next) break;
}
console.log();
}
// Create circular linked list: 2, 3, 4
let first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
let last = first.next.next;
last.next = first;
console.log("Original list: ");
printList(last);
// Delete a specific node
let key = 3;
last = deleteSpecificNode(last, key);
console.log("List after deleting node " + key + ": ");
printList(last);
OutputOriginal list: 2 3 4
List after deleting node 3: 2 4
Time Complexity: O(n), due to traversing the list to find the specific position
Auxiliary Space: O(1)
3. Deletion at the end of Circular linked list
To delete the last node in a circular linked list, we first check if the list is empty. If it is, we print a message and return nullptr. If the list contains only one node (where the head is the same as the last), we delete that node and set last to nullptr. For lists with multiple nodes, we need to traverse the list to find the second last node. We do this by starting from the head and moving through the list until we reach the node whose next pointer points to last. Once we find the second last node then we update its next pointer to point back to the head, this effectively removing the last node from the list. We then delete the last node to free up memory and return the updated last pointer, which now points to the last node.
Deletion at the end of Circular linked listStep-by-step approach:
- Check if List is Empty (last is nullptr) then print "List is empty, nothing to delete" and return nullptr.
- Get head node by Setting head to last->next.
- Check for single node, if head equals last, delete last and set last to nullptr.
- Find second last node by traversing the list until curr->next equals last.
- Update Pointer by setting curr->next to point to head.
- Delete last and update last to curr.
- Return the updated last.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node(int value) {
data = value;
next = nullptr;
}
};
// Function to delete the last node in the circular linked list
Node* deleteLastNode(Node* last) {
if (last == nullptr) {
// If the list is empty
cout << "List is empty, nothing to delete." << endl;
return nullptr;
}
Node* head = last->next;
// If there is only one node in the list
if (head == last) {
delete last;
last = nullptr;
return last;
}
// Traverse the list to find the second last node
Node* curr = head;
while (curr->next != last) {
curr = curr->next;
}
// Update the second last node's next pointer
// to point to head
curr->next = head;
delete last;
last = curr;
return last;
}
void printList(Node* last) {
if(last == NULL) return;
Node *head = last->next;
while (true){
cout << head->data << " ";
head = head->next;
if (head == last->next) break;
}
cout << endl;
}
int main() {
// Create circular linked list: 2, 3, 4
Node* first = new Node(2);
first->next = new Node(3);
first->next->next = new Node(4);
Node* last = first->next->next;
last->next = first;
cout << "Original list: ";
printList(last);
// Delete the last node
last = deleteLastNode(last);
cout << "List after deleting last node: ";
printList(last);
return 0;
}
C
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the circular linked list
struct Node {
int data;
struct Node* next;
};
// Function to delete the last node in the circular linked list
struct Node* deleteLastNode(struct Node* last) {
if (last == NULL) {
// If the list is empty
printf("List is empty, nothing to delete.\n");
return NULL;
}
struct Node* head = last->next;
// If there is only one node in the list
if (head == last) {
free(last);
last = NULL;
return last;
}
// Traverse the list to find the second last node
struct Node* curr = head;
while (curr->next != last) {
curr = curr->next;
}
// Update the second last node's next pointer to point to head
curr->next = head;
free(last);
last = curr;
return last;
}
void printList(struct Node* last) {
if (last == NULL) return;
struct Node* head = last->next;
while (1) {
printf("%d ", head->data);
head = head->next;
if (head == last->next) break;
}
printf("\n");
}
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
int main() {
// Create circular linked list: 2, 3, 4
struct Node* first = createNode(2);
first->next = createNode(3);
first->next->next = createNode(4);
struct Node* last = first->next->next;
last->next = first;
printf("Original list: ");
printList(last);
// Delete the last node
last = deleteLastNode(last);
printf("List after deleting last node: ");
printList(last);
return 0;
}
Java
class Node {
int data;
Node next;
Node(int value){
data = value;
next = null;
}
}
public class GFG {
public static Node deleteLastNode(Node last){
if (last == null) {
// If the list is empty
System.out.println(
"List is empty, nothing to delete.");
return null;
}
Node head = last.next;
// If there is only one node in the list
if (head == last) {
last = null;
return last;
}
// Traverse the list to find the second last node
Node curr = head;
while (curr.next != last) {
curr = curr.next;
}
// Update the second last node's next pointer to
// point to head
curr.next = head;
last = curr;
return last;
}
public static void printList(Node last){
if (last == null)
return;
Node head = last.next;
while (true) {
System.out.print(head.data + " ");
head = head.next;
if (head == last.next)
break;
}
System.out.println();
}
public static void main(String[] args){
// Create circular linked list: 2, 3, 4
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
System.out.print("Original list: ");
printList(last);
// Delete the last node
last = deleteLastNode(last);
System.out.print("List after deleting last node: ");
printList(last);
}
}
Python
class Node:
def __init__(self, data):
self.data = data
self.next = None
def deleteLastNode(last):
if last is None:
# If the list is empty
print("List is empty, nothing to delete.")
return None
head = last.next
# If there is only one node in the list
if head == last:
last = None
return last
# Traverse the list to find the second last node
curr = head
while curr.next != last:
curr = curr.next
# Update the second last node's next pointer to point to head
curr.next = head
last = curr
return last
def printList(last):
if last is None:
return
head = last.next
while True:
print(head.data, end=" ")
head = head.next
if head == last.next:
break
print()
# Create circular linked list: 2, 3, 4
first = Node(2)
first.next = Node(3)
first.next.next = Node(4)
last = first.next.next
last.next = first
print("Original list: ", end="")
printList(last)
# Delete the last node
last = deleteLastNode(last)
print("List after deleting last node: ", end="")
printList(last)
C#
using System;
public class Node {
public int data;
public Node next;
public Node(int value)
{
data = value;
next = null;
}
}
public class GFG {
// Function to delete the last node in the circular
// linked list
public static Node deleteLastNode(Node last)
{
if (last == null) {
// If the list is empty
Console.WriteLine(
"List is empty, nothing to delete.");
return null;
}
Node head = last.next;
// If there is only one node in the list
if (head == last) {
last = null;
return last;
}
// Traverse the list to find the second last node
Node curr = head;
while (curr.next != last) {
curr = curr.next;
}
// Update the second last node's next pointer
// to point to head
curr.next = head;
last = curr;
return last;
}
// Function to print the circular linked list
public static void printList(Node last)
{
if (last == null) {
Console.WriteLine("List is Empty");
return;
}
Node head = last.next;
while (true) {
Console.Write(head.data + " ");
head = head.next;
if (head == last.next)
break;
}
Console.WriteLine();
}
public static void Main(string[] args)
{
// Create circular linked list: 2, 3, 4
Node first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
Node last = first.next.next;
last.next = first;
Console.Write("Original list: ");
printList(last);
// Delete the last node
last = deleteLastNode(last);
Console.Write("List after deleting last node: ");
printList(last);
}
}
JavaScript
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
function deleteLastNode(last) {
if (last === null) {
// If the list is empty
console.log("List is empty, nothing to delete.");
return null;
}
let head = last.next;
// If there is only one node in the list
if (head === last) {
last = null;
return last;
}
// Traverse the list to find the second last node
let curr = head;
while (curr.next !== last) {
curr = curr.next;
}
// Update the second last node's next pointer to point to head
curr.next = head;
last = curr;
return last;
}
function printList(last) {
if (last === null) return;
let head = last.next;
while (true) {
process.stdout.write(head.data + " ");
head = head.next;
if (head === last.next) break;
}
console.log();
}
// Create circular linked list: 2, 3, 4
let first = new Node(2);
first.next = new Node(3);
first.next.next = new Node(4);
let last = first.next.next;
last.next = first;
console.log("Original list: ");
printList(last);
// Delete the last node
last = deleteLastNode(last);
console.log("List after deleting last node: ");
printList(last);
OutputOriginal list: 2 3 4
List after deleting last node: 2 3
Time Complexity: O(N)
Auxiliary Space: O(1)
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