0% found this document useful (0 votes)
2 views11 pages

Problem Statements

The document outlines various problems related to linked lists, including detecting cycles, reversing groups of nodes, finding intersection points, and deleting nodes without a head pointer. It provides problem statements, examples, and explanations for each problem, covering a wide range of linked list operations such as merging, sorting, and manipulating nodes. Additionally, it includes tasks involving doubly linked lists and binary trees, demonstrating the versatility of linked list data structures.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

Problem Statements

The document outlines various problems related to linked lists, including detecting cycles, reversing groups of nodes, finding intersection points, and deleting nodes without a head pointer. It provides problem statements, examples, and explanations for each problem, covering a wide range of linked list operations such as merging, sorting, and manipulating nodes. Additionally, it includes tasks involving doubly linked lists and binary trees, demonstrating the versatility of linked list data structures.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

• Detect Loop

Detect Cycle in Linked List


formatted exactly in the pattern and explanation style you asked for (like the red–
highlighted problem image).

Detect Cycle in Linked List


Last Updated: 28 Aug, 2025

Problem Statement
You are given the head of a singly linked list.
Determine whether the list contains a cycle.
A cycle exists if, while traversing the list through next pointers,
you encounter a node that has already been visited instead of eventually reaching null.

Example 1
Input:
head: 1 → 3 → 4 → 3
Output:
true
Explanation:
The last node of the linked list does not point to null;
instead, it points to an earlier node (3) in the list, creating a cycle.

Example 2
Input:
head: 1 → 8 → 3 → 4 → null
Output:
false
Explanation:
The last node of the linked list points to null,
indicating the end of the list and that no cycle exists.

• Length of the Loop

• Length of Cycle in Linked List


• Last Updated: 28 Aug, 2025
• Problem Statement
• Given the head of a singly linked list, determine the length of the cycle (loop) if one
exists.
• A cycle occurs when a node’s next pointer points to a previously visited node in the
list.
• If no cycle is present, return 0.
• Example 1
• Input:
Linked list representation → 2 → 4 → 6 → 8 → 10 → 6 (loop back)
• Output:
3
• Explanation:
There exists a loop in the linked list, and the length of the loop is 3 (the nodes 6 → 8
→ 10 → 6).
• Example 2
• Input:
Linked list representation → 1 → 2 → 3 → 4 → null
• Output:
0
• Explanation:
The linked list ends normally at null, so no cycle exists.

• Reverse in groups
• You are given the head of a singly linked list and an integer k.
You need to reverse every k nodes of the linked list and return the head of the
modified list.
• Note:
If the number of nodes is not a multiple of k, then the remaining nodes at the end
should also be reversed as a group.
• Example 1
• Input:
Linked List: 1 → 2 → 3 → 4 → 5 → 6
k=2
• Output:
2→1→4→3→6→5
• Explanation:
The linked list is reversed in groups of size k = 2.
• Example 2
• Input:
Linked List: 1 → 2 → 3 → 4 → 5 → 6
k=4
• Output:
4→3→2→1→6→5
• Explanation:
The first 4 nodes are reversed, and the remaining nodes (5 → 6) form the last group,
which is also reversed.

• Intersection Point
• Given the head of two singly linked lists that merge at some point to form a Y-shaped
structure. The two lists may have different lengths and contain distinct nodes initially,
but from the intersection point onward, they share the same sequence of nodes. Find
the node at which the two linked lists first intersect.
• Example:
• Input:
List A: 4 → 1 → 8 → 4 → 5
List B: 5 → 6 → 1 → 8 → 4 → 5
• Output:
8
• Explanation:
8 is the first common node where both linked lists start sharing the same sequence of
nodes.

• Delete without Head pointer


Delete a Node from Linked List Without Head Pointer
Last Updated : 11 Jul, 2025
You are given a singly linked list and a pointer which is pointing to the node that needs to
be deleted. You are not given the head pointer or any reference to other nodes. You need
to write a function to delete that specific node from the linked list.
Your function will take only one argument — a pointer to the node which is to be deleted.
Note:
• No head reference is given.
• It is guaranteed that the node to be deleted is not the last node.
Example 1:
Input: C (a pointer to node C)
Output: A → B → D → E → F
Example 2:
Input: A (a pointer to node A)
Output: B → C → D → E → F

• Merge two sorted linked lists


• Merge Two Sorted Linked Lists
Last Updated : 30 Aug, 2025
• Given the heads of two sorted linked lists, merge them into a single sorted linked list
and return the head of the merged list.
• Examples:
• Input 1:
List1: 5 → 10 → 15 → 40
List2: 2 → 3 → 20
• Output 1:
2 → 3 → 5 → 10 → 15 → 20 → 40
• Explanation:
Merging two sorted lists [5, 10, 15, 40] and [2, 3, 20] in order gives 2 → 3 → 5 → 10
→ 15 → 20 → 40.
• Input 2:
List1: 1 → 1
List2: 2 → 4
• Output 2:
1→1→2→4
• Explanation:
Merging [1, 1] and [2, 4] in order gives 1 → 1 → 2 → 4.

• Sort a List of 0s, 1s and 2s


Problem Statement:
Given the head of a singly linked list containing nodes with values only 0, 1, and 2,
your task is to sort the linked list in non-decreasing order (all 0s first, followed by 1s, then
2s).
You must modify the linked list such that the nodes are rearranged according to their
values, without using any extra space for another list.
Example 1:
Input: 1 → 1 → 2 → 0 → 2 → 0 → 1 → NULL
Output: 0 → 0 → 1 → 1 → 1 → 2 → 2 → NULL
Example 2:
Input: 1 → 1 → 2 → 1 → 0 → NULL
Output: 0 → 1 → 1 → 1 → 2 → NULL
Expected Approach:
• Count the occurrences of 0s, 1s, and 2s in the linked list.
• Overwrite the node values in sequence: first 0s, then 1s, and finally 2s.
Time Complexity: O(n)
Space Complexity: O(1)

• Palindrome Linked List


Problem Statement:
Given the head of a singly linked list, determine whether the linked list is a
palindrome.
A linked list is considered a palindrome if the sequence of node values is the same when read
both forward and backward.
Example 1:
Input: 1 → 2 → 3 → 2 → 1
Output: true
Explanation:
The linked list reads the same from front to back, so it is a palindrome.
Example 2:
Input: 1 → 2 → 3 → 4
Output: false
Explanation:
The linked list does not read the same in both directions, so it is not a palindrome.

• Remove all occurrences of a given list


• Problem Statement:
• Given the head of a singly linked list and an integer key, delete all occurrences of the
given key from the linked list and return the updated list.
• Example 1:
Input: head = 2 → 2 → 1 → 8 → 2 → NULL, key = 2
Output: 1 → 8 → NULL
• Explanation:
All nodes containing the value 2 are removed from the linked list.
• Example 2:
Input: head = 1 → 1 → 1 → 4 → 1 → NULL, key = 1
Output: 4 → NULL
• Explanation:
All nodes containing the value 1 are deleted from the linked list.

• Split a Circular Linked List into two halves


• Problem Statement:
• Given the head of a circular linked list, split it into two halves such that both
resulting linked lists are also circular.
If the number of nodes in the original list is odd, then the first circular list should
contain one extra node than the second.
• Example 1:
Input: 10 → 4 → 9 (circular)
Output:
First List: 10 → 4 (circular)
Second List: 9 (circular)
• Explanation:
The number of nodes is odd, so the first list has one extra node compared to the
second.
• Example 2:
Input: 10 → 4 → 9 → 7 (circular)
Output:
First List: 10 → 4 (circular)
Second List: 9 → 7 (circular)
• Explanation:
The number of nodes is even, so the list is split into two equal circular halves.

• Pair Sum in Doubly Linked List


• Problem Statement:
• Given the head of a sorted doubly linked list containing positive and distinct
elements, find all pairs of nodes whose sum equals a given value x.
The pairs should be returned in sorted order based on the first element of each pair.
• Example 1:
Input: 1 ↔ 2 ↔ 3 ↔ 4 ↔ 5 ↔ 6, x = 7
Output: (1, 6), (2, 5)
• Explanation:
There are two pairs whose sum is equal to 7: (1, 6) and (2, 5).
• Example 2:
Input: 1 ↔ 2 ↔ 3 ↔ 4 ↔ 5, x = 6
Output: (1, 5)
• Explanation:
Only one pair (1, 5) has a sum equal to 6.

• Add two numbers represented by Linked lists


Problem Statement:
Given two non-negative integers represented as linked lists, where each node contains a
single digit, return a new linked list representing their sum.
The digits are stored in the same order as they appear in the number (most significant digit
first).
Note:
• The input lists may contain leading zeros, but the resulting linked list must not
contain any leading zeros.
Example 1:
Input:
List1: 1 → 2 → 3
List2: 9 → 9 → 9
Output:
1→1→2→2
Explanation:
The sum of 123 and 999 is 1122.
Example 2:
Input:
List1: 6 → 3
List2: 7
Output:
7→0
Explanation:
The sum of 63 and 7 is 70.

• Multiply two numbers represented by Linked Lists


• Problem Statement:
• Given the heads of two linked lists representing two non-negative integers, return the
product of the two numbers as an integer.
Each node in the linked list contains a single digit, and the digits are stored in the
same order as they appear in the number (most significant digit first).
• Example 1:
Input:
head1: 1 → 0 → 0
head2: 1 → 0
• Output:
1000
• Explanation:
The first linked list represents 100, and the second represents 10.
100 × 10 = 1000.
• Example 2:
Input:
head1: 3 → 2
head2: 2
• Output:
64
• Explanation:
The first linked list represents 32, and the second represents 2.
32 × 2 = 64.

• Swap Kth nodes from beginning and end


• Problem Statement:
• Given the head of a singly linked list and an integer k, the task is to swap the k-th
node from the beginning with the k-th node from the end of the list and return the
updated list.
• Example 1:
Input: head = 5 → 10 → 8 → 5 → 9 → 3, k = 2
Output: 5 → 9 → 8 → 5 → 10 → 3
• Explanation:
The 2nd node from the start is 10 and the 2nd node from the end is 9.
After swapping, the linked list becomes 5 → 9 → 8 → 5 → 10 → 3.
• Example 2:
Input: head = 1 → 2 → 3 → 4 → 5, k = 1
Output: 5 → 2 → 3 → 4 → 1
• Explanation:
The 1st node from the start (1) and the 1st node from the end (5) are swapped.
The resulting list is 5 → 2 → 3 → 4 → 1.

• Rotate Doubly linked list by N nodes


• Problem Statement:
• Given the head of a doubly linked list and an integer p, rotate the linked list counter-
clockwise by p nodes.
Here, p is a positive integer and is always smaller than the total number of nodes in
the list.
• After rotation, the node at position p+1 becomes the new head of the list.
• Example 1:
Input: 1 ↔ 2 ↔ 3 ↔ 4 ↔ 5, p = 2
Output: 3 ↔ 4 ↔ 5 ↔ 1 ↔ 2
• Explanation:
After rotating the list by 2 nodes, the new head becomes the node with value 3.
• Example 2:
Input: 1 ↔ 2 ↔ 3 ↔ 4 ↔ 5, p = 3
Output: 4 ↔ 5 ↔ 1 ↔ 2 ↔ 3
• Explanation:
After rotating the list by 3 nodes, the new head becomes the node with value 4.

• Binary Tree to Doubly Linked List


Problem Statement:
Given the root of a Binary Tree, the task is to convert it into a Doubly Linked List (DLL)
in such a way that the order of nodes in the DLL is the same as the inorder traversal of the
given Binary Tree.
In the converted DLL:
• The left pointer of a tree node should act as the previous pointer in the DLL.
• The right pointer should act as the next pointer in the DLL.
• The leftmost node of the binary tree (the first node in inorder traversal) should
become the head of the DLL.

• Linked List from a 2D matrix


Given a matrix mat of size n × n, the task is to construct a 2D linked list representation of
the given matrix.
Each node of the linked list should contain four fields:
• data → stores the matrix element
• right → pointer to the next node in the same row
• down → pointer to the next node in the same column
Example 1:
Input:
mat = [[1 2 3]
[4 5 6]
[7 8 9]]
Output:
Construct-a-linked-list-from-2D-matrix-1
Example 2:
Input:
mat = [[23 28]
[23 28]]
Output:
Construct-a-linked-list-from-2D-matrix-2

• Reverse a Sublist
• Given a singly linked list and two positions m and n, the task is to reverse the linked
list from position m to n.
• Example 1:
• Input:
linkedlist: 10 → 20 → 30 → 40 → 50 → 60 → 70 → NULL,
m = 3, n = 6
• Output:
10 → 20 → 60 → 50 → 40 → 30 → 70 → NULL
• Explanation:
The linked list is reversed starting from node m = 30 to n = 60.
• Example 2:
• Input:
linkedlist: 1 → 2 → 3 → 4 → 5 → 6 → NULL,
m = 2, n = 4
• Output:
1 → 4 → 3 → 2 → 5 → 6 → NULL
• Explanation:
The linked list is reversed starting from node m = 2 to n = 4.
• Delete N nodes after M
Given a singly linked list and two integers m and n, the task is to traverse the linked list such
that you skip m nodes, then delete the next n nodes, and repeat this process until the end of
the linked list.
Note:
• The value of m cannot be 0.
Example 1:
Input:
Linked List: 9 → 1 → 3 → 5 → 9 → 4 → 10 → 1
m = 2, n = 1
Output:
9 → 1 → 5 → 9 → 10 → 1
Explanation:
After skipping 2 nodes (9, 1), the next node (3) is deleted.
Then again, skip 2 nodes (5, 9) and delete the next (4), and continue this till the end.
Example 2:
Input:
Linked List: 1 → 2 → 3 → 4 → 5 → 6
m = 6, n = 1
Output:
1→2→3→4→5→6
Explanation:
After skipping 6 nodes, we reach the end of the list. Hence, no nodes are deleted, and the
original list remains unchanged.

• Rearrange a given linked list in-place


• Given a singly linked list L₀ → L₁ → … → Lₙ₋₁ → Lₙ, rearrange the nodes in-place
to form a new list in the following order:
L₀ → Lₙ → L₁ → Lₙ₋₁ → L₂ → Lₙ₋₂ …
• You must perform this rearrangement in-place, without changing the values of the
nodes (only modify pointers).
• Example 1:
• Input:
1→2→3→4
• Output:
1→4→2→3
• Explanation:
The rearranged order becomes L₀ → L₃ → L₁ → L₂.
• Example 2:
• Input:
1→2→3→4→5
• Output:
1→5→2→4→3
• Explanation:
The rearranged order becomes L₀ → L₄ → L₁ → L₃ → L₂.

• Partition around a given value


Given a singly linked list and an integer x, partition the list so that:
• All nodes with values less than x come first,
• Followed by nodes with values equal to x,
• And finally, nodes with values greater than x.
The relative order of nodes within each partition must remain the same as in the original list.
Example 1:
Input:
1 → 4 → 3 → 2 → 5 → 2 → 3, x = 3
Output:
1→2→2→3→3→4→5
Explanation:
All nodes less than 3 appear first, followed by nodes equal to 3, and then nodes greater than 3
— while keeping their original order.
Example 2:
Input:
10 → 4 → 20 → 10 → 3, x = 3
Output:
3 → 10 → 4 → 20 → 10

You might also like