Modify contents of Linked List
Last Updated :
19 Feb, 2025
Given a Singly linked list. The task is to modify the value of the first half of nodes such that 1st node's new value is equal to the value of the last node minus the first node's current value, 2nd node's new value is equal to the second last node's value minus 2nd nodes current value, likewise for first half nodes, then replace the second half of nodes with the initial values of the first half of nodes (values before modifying the nodes).
Note: If the size of it is odd then the value of the middle node remains unchanged.
Examples:
Input: head: 10 -> 4 -> 5 -> 3 -> 6
Output: -4 -> -1 -> 5 -> 4 -> 10

Explanation: After modifying first half, list will be: -4 -> -1 -> 5 -> 3 -> 6
After modifying Second half also, list will be: -4 -> -1 -> 5 -> 4 -> 10
Input: head: 2 -> 9 -> 8 -> 12 -> 7 -> 10
Output: 8 -> -2 -> 4 -> 8 -> 9 -> 2
Explanation: After modifying the linked list as required, we have a new linked list containing the elements as 8 -> -2 -> 4 -> 8 -> 9 -> 2.
[Naive Approach] Change Linked List to Array - O(n) Time and O(n) Space
The idea is to take out all elements from LinkedList add them into an array and apply the required operations:
- For each node in the first half of the list, its value is changed by subtracting it from the corresponding value in the second half of the list (from the end towards the center).
- After performing these modifications, the values in the first half of the array are swapped with the corresponding values in the second half on the array and then put back all elements into the LinkedList.
C++
// C++ program to modify a singly linked list
// By transferring it to array
#include <iostream>
#include <vector>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int x) {
data = x;
next = nullptr;
}
};
// Function to count the number of nodes
// in the linked list
int countNodes(Node* head) {
int count = 0;
Node* curr = head;
while (curr != nullptr) {
count++;
curr = curr->next;
}
return count;
}
// Function to convert linked list to vector
void linkedListToVector(Node* head, vector<int>& vec) {
Node* curr = head;
for (int i = 0; i < vec.size(); ++i) {
vec[i] = curr->data;
curr = curr->next;
}
}
// Function to convert vector back to linked list
void vectorToLinkedList(const vector<int>& vec, Node* head) {
Node* curr = head;
for (int i = 0; i < vec.size(); ++i) {
curr->data = vec[i];
curr = curr->next;
}
}
// Function to modify the vector according to
// the logic provided
void modifyVector(vector<int>& vec) {
// Modify the vector
for (int i = 0; i < vec.size() / 2; ++i) {
int x = vec[i];
vec[i] = vec[vec.size() - i - 1] - x;
vec[vec.size() - i - 1] = x;
}
}
// Function to modify the linked list
Node* modifyTheList(Node* head) {
if (!head->next) return nullptr;
// Count the number of nodes
int n = countNodes(head);
// Create a vector for the linked list data
vector<int> vec(n);
// Convert linked list to vector
linkedListToVector(head, vec);
// Modify the vector
modifyVector(vec);
// Convert vector back to linked list
vectorToLinkedList(vec, head);
return head;
}
// Function to print the linked list
void printList(Node* node) {
Node* curr = node;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
// Create a hard-coded linked list
// 10 -> 4 -> 5 -> 3 -> 6
Node* head = new Node(10);
head->next = new Node(4);
head->next->next = new Node(5);
head->next->next->next = new Node(3);
head->next->next->next->next = new Node(6);
head = modifyTheList(head);
printList(head);
return 0;
}
Java
// Java program to modify a singly linked list
// By transferring it to List
import java.util.ArrayList;
import java.util.List;
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Function to count the number of nodes in
// the linked list
static int countNodes(Node head) {
int count = 0;
Node curr = head;
while (curr != null) {
count++;
curr = curr.next;
}
return count;
}
// Function to convert linked list to List
static void linkedListToList(Node head, List<Integer> list) {
Node curr = head;
for (int i = 0; i < list.size(); ++i) {
list.set(i, curr.data);
curr = curr.next;
}
}
// Function to convert List back to linked list
static void listToLinkedList(List<Integer> list, Node head) {
Node curr = head;
for (int i = 0; i < list.size(); ++i) {
curr.data = list.get(i);
curr = curr.next;
}
}
// Function to modify the List according to the logic provided
static void modifyList(List<Integer> list) {
// Modify the List
for (int i = 0; i < list.size() / 2; ++i) {
int x = list.get(i);
list.set(i, list.get(list.size() - i - 1) - x);
list.set(list.size() - i - 1, x);
}
}
// Function to modify the linked list
static Node modifyTheList(Node head) {
if (head.next == null) return null;
// Count the number of nodes
int n = countNodes(head);
// Create a List for the linked list data
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
list.add(0);
}
// Convert linked list to List
linkedListToList(head, list);
// Modify the List
modifyList(list);
// Convert List back to linked list
listToLinkedList(list, head);
return head;
}
// Function to print the linked list
static void printList(Node node) {
Node curr = node;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Create a hard-coded linked list
// 10 -> 4 -> 5 -> 3 -> 6
Node head = new Node(10);
head.next = new Node(4);
head.next.next = new Node(5);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(6);
head = modifyTheList(head);
printList(head);
}
}
Python
# Python program to modify a singly linked list
# By transferring it to an array
class Node:
def __init__(self, data):
self.data = data
self.next = None
def countNodes(head):
count = 0
curr = head
while curr is not None:
count += 1
curr = curr.next
return count
def linkedListToArray(head, arr, n):
curr = head
for i in range(n):
arr[i] = curr.data
curr = curr.next
def arrayToLinkedList(arr, head, n):
curr = head
for i in range(n):
curr.data = arr[i]
curr = curr.next
def modifyArray(arr, n):
# Modify the array
for i in range(n // 2):
x = arr[i]
arr[i] = arr[n - i - 1] - x
arr[n - i - 1] = x
def modifyTheList(head):
if head.next is None:
return None
# Count the number of nodes
n = countNodes(head)
# Create an array
arr = [0] * n
# Convert linked list to array
linkedListToArray(head, arr, n)
# Modify the array
modifyArray(arr, n)
# Convert array back to linked list
arrayToLinkedList(arr, head, n)
return head
def printList(node):
curr = node
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
print()
if __name__ == "__main__":
# Create a hard-coded linked list
# 10 -> 4 -> 5 -> 3 -> 6
head = Node(10)
head.next = Node(4)
head.next.next = Node(5)
head.next.next.next = Node(3)
head.next.next.next.next = Node(6)
head = modifyTheList(head)
printList(head)
C#
// C# program to modify a singly linked list
// By transferring it to List
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node next;
public Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Function to count the number of nodes
// in the linked list
static int CountNodes(Node head) {
int count = 0;
Node curr = head;
while (curr != null) {
count++;
curr = curr.next;
}
return count;
}
// Function to convert linked list to List
static void LinkedListToList(Node head, List<int> list) {
Node curr = head;
while (curr != null) {
list.Add(curr.data);
curr = curr.next;
}
}
// Function to convert List back to linked list
static void ListToLinkedList(List<int> list, Node head) {
Node curr = head;
for (int i = 0; i < list.Count; ++i) {
curr.data = list[i];
curr = curr.next;
}
}
// Function to modify the List according
// to the logic provided
static void ModifyList(List<int> list) {
int n = list.Count;
for (int i = 0; i < n / 2; ++i) {
int x = list[i];
list[i] = list[n - i - 1] - x;
list[n - i - 1] = x;
}
}
// Function to modify the linked list
static Node ModifyTheList(Node head) {
if (head.next == null) return head;
// Create a List to hold the data
List<int> list = new List<int>();
// Convert linked list to List
LinkedListToList(head, list);
// Modify the List
ModifyList(list);
// Convert List back to linked list
ListToLinkedList(list, head);
return head;
}
// Function to print the linked list
static void PrintList(Node node) {
Node curr = node;
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main(string[] args) {
// Create a hard-coded linked list
// 10 -> 4 -> 5 -> 3 -> 6
Node head = new Node(10);
head.next = new Node(4);
head.next.next = new Node(5);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(6);
head = ModifyTheList(head);
PrintList(head);
}
}
JavaScript
// JavaScript program to modify a singly linked list
// By transferring it to a list
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Function to count the number of nodes in
// the linked list
function countNodes(head) {
let count = 0;
let curr = head;
while (curr !== null) {
count++;
curr = curr.next;
}
return count;
}
// Function to convert linked list to list
function linkedListToList(head, list) {
let curr = head;
while (curr !== null) {
list.push(curr.data);
curr = curr.next;
}
}
// Function to convert list back to linked list
function listToLinkedList(list, head) {
let curr = head;
for (let i = 0; i < list.length; ++i) {
curr.data = list[i];
curr = curr.next;
}
}
// Function to modify the list according
// to the logic provided
function modifyList(list) {
let n = list.length;
for (let i = 0; i < Math.floor(n / 2); ++i) {
let x = list[i];
list[i] = list[n - i - 1] - x;
list[n - i - 1] = x;
}
}
// Function to modify the linked list
function modifyTheList(head) {
if (head.next === null) return head;
// Count the number of nodes
let n = countNodes(head);
// Create a list
let list = [];
// Convert linked list to list
linkedListToList(head, list);
// Modify the list
modifyList(list);
// Convert list back to linked list
listToLinkedList(list, head);
return head;
}
// Function to print the linked list
function printList(node) {
let curr = node;
while (curr !== null) {
console.log(curr.data + " ");
curr = curr.next;
}
console.log();
}
// Create a hard-coded linked list
// 10 -> 4 -> 5 -> 3 -> 6
let head = new Node(10);
head.next = new Node(4);
head.next.next = new Node(5);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(6);
head = modifyTheList(head);
printList(head);
[Expected Approach] Reverse the 2nd Half Twice - O(n) Time and O(1) Space
The idea is to handle the modification of a singly linked list by reversing its second half to allow traversal in both directions.
- First, find the middle of the list and reverse the second half starting from the node after the middle.
- Then, traverse the first half and the reversed second half simultaneously, updating the first half's node values by subtracting their values from the corresponding nodes in the second half and swapping them.
- After the traversal, reverse the modified second half again to restore its original order.
- Finally, reconnect the first and restored second halves to complete the list modification.
C++
// C++ program to modify a singly linked list
// By Reversing the 2nd Half Twice
#include <iostream>
#include <vector>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int x) {
data = x;
next = nullptr;
}
};
// Function to reverse a linked list iteratively
Node* reverse(Node* head) {
Node* prev = nullptr;
Node* curr = head;
Node* next = nullptr;
while (curr != nullptr) {
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to modify the linked list
Node* modifyTheList(Node* head) {
if (!head->next) {
return head;
}
Node* slow = head;
Node* fast = head;
Node* mid;
// Finding the middle node of the linked list
while (fast->next && fast->next->next) {
slow = slow->next;
fast = fast->next->next;
}
mid = slow;
Node* reversedList = mid->next;
// Splitting the list into two halves
mid->next = nullptr;
// Reversing the second half of the list
reversedList = reverse(reversedList);
Node* curr1 = head;
Node* curr2 = reversedList;
vector<int> firstHalf, secondHalf;
// Copying the data into vectors
while (curr1 != nullptr) {
firstHalf.push_back(curr1->data);
curr1 = curr1->next;
}
while (curr2 != nullptr) {
secondHalf.push_back(curr2->data);
curr2 = curr2->next;
}
// Modifying the vector values
for (int i = 0; i < secondHalf.size(); i++) {
int x = firstHalf[i];
firstHalf[i] = secondHalf[i] - x;
secondHalf[i] = x;
}
// Converting vectors back to linked list
curr1 = head;
for (int val : firstHalf) {
curr1->data = val;
curr1 = curr1->next;
}
curr2 = reversedList;
for (int val : secondHalf) {
curr2->data = val;
curr2 = curr2->next;
}
// Reversing the second half again and
// connecting both halves
mid->next = reverse(reversedList);
return head;
}
// Function to print the linked list
void printList(Node* node) {
Node* curr = node;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
}
int main() {
// Create a hard-coded linked list
// 10 -> 4 -> 5 -> 3 -> 6
Node* head = new Node(10);
head->next = new Node(4);
head->next->next = new Node(5);
head->next->next->next = new Node(3);
head->next->next->next->next = new Node(6);
head = modifyTheList(head);
printList(head);
return 0;
}
C
// C program to modify a singly linked list
// By Reversing the 2nd Half Twice
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Function to reverse a linked list iteratively
struct Node* reverse(struct Node* head) {
// Initialize previous, current, and next pointers
struct Node* prev = NULL;
struct Node* curr = head;
struct Node* next = NULL;
// Traverse and reverse the linked list
while (curr != NULL) {
next = curr->next;
// Reverse the current node's pointer
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to modify the linked list
struct Node* modifyTheList(struct Node* head) {
// Returning head if list has only one node
if (head->next == NULL) return head;
struct Node* slow = head;
struct Node* fast = head;
struct Node* mid;
// Finding the middle node of the linked list
while (fast->next != NULL && fast->next->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
mid = slow;
// Storing the second half of the list starting
// after the middle node
struct Node* reversedList = mid->next;
// Splitting the list into two halves
mid->next = NULL;
// Reversing the second half of the list
reversedList = reverse(reversedList);
struct Node* curr1 = head;
struct Node* curr2 = reversedList;
// Iterating over both halves of the list
// and modifying the values
while (curr2 != NULL) {
int x = curr1->data;
curr1->data = curr2->data - x;
curr2->data = x;
curr1 = curr1->next;
curr2 = curr2->next;
}
// Reversing the second half of the list
// again and connecting both halves
mid->next = reverse(reversedList);
return head;
}
// Function to print the linked list
void printList(struct Node* node) {
struct Node* curr = node;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
}
struct Node* createNode(int new_data) {
struct Node* new_node
= (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
int main() {
// Create a hard-coded linked list
// 10 -> 4 -> 5 -> 3 -> 6
struct Node* head = createNode(10);
head->next = createNode(4);
head->next->next = createNode(5);
head->next->next->next = createNode(3);
head->next->next->next->next = createNode(6);
head = modifyTheList(head);
printList(head);
return 0;
}
Java
// Java program to modify a singly linked list
// By Reversing the 2nd Half Twice
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Function to reverse a linked list iteratively
static Node reverse(Node head) {
// Initialize previous, current,
// and next pointers
Node prev = null;
Node curr = head;
Node next = null;
// Traverse and reverse the linked list
while (curr != null) {
next = curr.next;
// Reverse the current node's pointer
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to modify the linked list
static Node modifyTheList(Node head) {
// Returning head if list has only one node
if (head.next == null) return head;
Node slow = head;
Node fast = head;
Node mid;
// Finding the middle node of the linked list
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
mid = slow;
// Storing the second half of the list starting
// after the middle node
Node reversedList = mid.next;
// Splitting the list into two halves
mid.next = null;
// Reversing the second half of the list
reversedList = reverse(reversedList);
Node curr1 = head;
Node curr2 = reversedList;
// Iterating over both halves of the list
// and modifying the values
while (curr2 != null) {
int x = curr1.data;
curr1.data = curr2.data - x;
curr2.data = x;
curr1 = curr1.next;
curr2 = curr2.next;
}
// Reversing the second half of the list
// again and connecting both halves
mid.next = reverse(reversedList);
return head;
}
// Function to print the linked list
static void printList(Node node) {
Node curr = node;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
}
public static void main(String[] args) {
// Create a hard-coded linked list
// 10 -> 4 -> 5 -> 3 -> 6
Node head = new Node(10);
head.next = new Node(4);
head.next.next = new Node(5);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(6);
head = modifyTheList(head);
printList(head);
}
}
Python
# Python program to modify a singly linked list
# By Reversing the 2nd Half Twice
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Function to reverse a linked list iteratively
def reverse(head):
# Initialize previous, current,
# and next pointers
prev = None
curr = head
# Traverse and reverse the linked list
while curr is not None:
next_node = curr.next
# Reverse the current node's pointer
curr.next = prev
prev = curr
curr = next_node
return prev
# Function to modify the linked list
def modifyTheList(head):
# Returning head if list has only one node
if head.next is None:
return head
slow = head
fast = head
# Finding the middle node of the linked list
while fast.next is not None and fast.next.next is not None:
slow = slow.next
fast = fast.next.next
mid = slow
# Storing the second half of the list starting
# after the middle node
reversed_list = mid.next
# Splitting the list into two halves
mid.next = None
# Reversing the second half of the list
reversed_list = reverse(reversed_list)
curr1 = head
curr2 = reversed_list
# Iterating over both halves of the list
# and modifying the values
while curr2 is not None:
x = curr1.data
curr1.data = curr2.data - x
curr2.data = x
curr1 = curr1.next
curr2 = curr2.next
# Reversing the second half of the list
# again and connecting both halves
mid.next = reverse(reversed_list)
return head
def print_list(node):
curr = node
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
if __name__ == "__main__":
# Create a hard-coded linked list
# 10 -> 4 -> 5 -> 3 -> 6
head = Node(10)
head.next = Node(4)
head.next.next = Node(5)
head.next.next.next = Node(3)
head.next.next.next.next = Node(6)
head = modifyTheList(head)
print_list(head)
C#
// C# program to modify a singly linked list
// By Reversing the 2nd Half Twice
using System;
class Node {
public int data;
public Node next;
public Node(int x) {
data = x;
next = null;
}
}
class GfG {
// Function to reverse a linked
// list iteratively
static Node Reverse(Node head) {
Node prev = null;
Node curr = head;
Node next = null;
// Traverse and reverse the linked list
while (curr != null ){
next = curr.next;
// Reverse the current node's pointer
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to modify the linked list
static Node ModifyTheList(Node head) {
// Returning if list has only one node
if (head.next == null) return head;
Node slow = head;
Node fast = head;
Node mid;
// Finding the middle node of the linked list
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
mid = slow;
// Storing the second half of the list starting
// after the middle node
Node reversedList = mid.next;
// Splitting the list into two halves
mid.next = null;
// Reversing the second half of the list
reversedList = Reverse(reversedList);
Node curr1 = head;
Node curr2 = reversedList;
// Iterating over both halves of the list
// and modifying the values
while (curr2 != null) {
int x = curr1.data;
curr1.data = curr2.data - x;
curr2.data = x;
curr1 = curr1.next;
curr2 = curr2.next;
}
// Reversing the second half of the list
// again and connecting both halves
mid.next = Reverse(reversedList);
return head;
}
// Function to print the linked list
static void PrintList(Node node) {
Node curr = node;
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main(string[] args) {
// Create a hard-coded linked list
// 10 -> 4 -> 5 -> 3 -> 6
Node head = new Node(10);
head.next = new Node(4);
head.next.next = new Node(5);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(6);
head = ModifyTheList(head);
PrintList(head);
}
}
JavaScript
// Javascript program to modify a singly linked list
// By Reversing the 2nd Half Twice
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Function to reverse a linked list iteratively
function reverse(head) {
let prev = null;
let curr = head;
let next = null;
// Traverse and reverse the linked list
while (curr !== null) {
next = curr.next;
// Reverse the current node's pointer
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
// Function to modify the linked list
function modifyTheList(head) {
// Returning if list has only one node
if (head.next === null) { return head; }
let slow = head;
let fast = head;
// Finding the middle node of the linked list
while (fast.next !== null && fast.next.next !== null) {
slow = slow.next;
fast = fast.next.next;
}
let mid = slow;
// Storing the second half of the list starting
// after the middle node
let reversedList = mid.next;
// Splitting the list into two halves
mid.next = null;
// Reversing the second half of the list
reversedList = reverse(reversedList);
let curr1 = head;
let curr2 = reversedList;
// Iterating over both halves of the list
// and modifying the values
while (curr2 !== null) {
let x = curr1.data;
curr1.data = curr2.data - x;
curr2.data = x;
curr1 = curr1.next;
curr2 = curr2.next;
}
// Reversing the second half of the list
// again and connecting both halves
mid.next = reverse(reversedList);
return head;
}
// Function to print the linked list
function printList(node) {
let curr = node;
while (curr !== null) {
console.log(curr.data + " ");
curr = curr.next;
}
console.log();
}
// Create a hard-coded linked list
// 10 -> 4 -> 5 -> 3 -> 6
let head = new Node(10);
head.next = new Node(4);
head.next.next = new Node(5);
head.next.next.next = new Node(3);
head.next.next.next.next = new Node(6);
head = modifyTheList(head);
printList(head);
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