Skip to content

Commit 8ea37b9

Browse files
committed
Improve LeetCode-style docstrings in list tasks
1 parent 8481c6d commit 8ea37b9

9 files changed

Lines changed: 132 additions & 143 deletions

File tree

src/4_Lists/clone_complex_list.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
11
/*
22
* Task: Deep copy a linked list with sibling pointers.
33
*
4-
* DEEP COPY OF A COMPLEX LINKED LIST WITH SIBLING POINTERS
4+
* COPY LIST WITH RANDOM (SIBLING) POINTER
55
*
6-
* Define a class named ComplexList that represents a singly linked list where
7-
* each node has an integer value, a pointer to the next node, and an additional
8-
* "sibling" pointer that can reference any other node in the list. The class
9-
* supports operations for appending nodes, setting sibling pointers, and
10-
* performing a deep copy of the entire list while preserving the sibling
11-
* relationships.
6+
* Problem:
7+
* Given a linked list where each node has `next` and `sibling` pointers,
8+
* create a deep copy of the list. The copied nodes must be new objects, and
9+
* every copied sibling pointer must reference the corresponding copied node.
1210
*
13-
* ASCII Illustration:
11+
* Constraints:
12+
* - 0 <= n <= 10^5
13+
* - Node values are 32-bit signed integers.
14+
* - `sibling` is either null or points to any node in the same list.
1415
*
15-
* Original List: Copied List:
16+
* Example 1:
17+
* Input: values = [1,2,3,4,5], sibling = {1->3, 2->5, 5->2}
18+
* Output: deep-copied list with identical `next` and `sibling` structure
1619
*
17-
* 1 --> 2 --> 3 1 --> 2 --> 3
18-
* \ \ \ \
19-
* v v v v
20-
* 3 5 3 5
21-
*
22-
* Example:
23-
* Input List:
24-
* Node values: 1 -> 2 -> 3 -> 4 -> 5
25-
* Sibling relationships:
26-
* - 1's sibling -> 3
27-
* - 2's sibling -> 5
28-
* - 5's sibling -> 2
29-
*
30-
* When copied, both lists will have the same structure and sibling connections.
20+
* Example 2:
21+
* Input: values = [1], sibling = {}
22+
* Output: [1] (copied node, not the same address)
3123
*/
3224

3325
#include "list.h"

src/4_Lists/delete_duplicates.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
/*
22
* Task: Delete duplicate values from a singly linked list.
33
*
4-
* DELETE DUPLICATE NODES IN A LINKED LIST
4+
* REMOVE DUPLICATES FROM AN UNSORTED LINKED LIST
55
*
6-
* Define a class named UniqueList that extends a basic singly linked list
7-
* (from list.h) and provides an operation to delete duplicate nodes from the
8-
* list. The deletion process is implemented by traversing the list and using
9-
* an unordered_set to keep track of seen values. When a duplicate node is
10-
* encountered, it is removed from the list.
6+
* Problem:
7+
* Given the head of an unsorted singly linked list, remove duplicate values so
8+
* that each value appears only once. Keep the first occurrence of each value.
119
*
12-
* ASCII Illustration:
10+
* Constraints:
11+
* - 0 <= n <= 10^5
12+
* - Node values are 32-bit signed integers.
1313
*
14-
* Before: 1 -> 2 -> 3 -> 4 -> 4 -> 5
15-
* | |
16-
* +---------+
14+
* Example 1:
15+
* Input: head = [1,2,3,4,4,5]
16+
* Output: [1,2,3,4,5]
1717
*
18-
* After: 1 -> 2 -> 3 -> 4 -> 5
19-
*
20-
* Example Input/Output:
21-
* Input List: 1, 2, 3, 4, 4, 5
22-
* Output List: 1, 2, 3, 4, 5
23-
*
24-
* Explanation:
25-
* The duplicate node containing 4 is removed so that each value appears only
26-
* once.
18+
* Example 2:
19+
* Input: head = [1,1,1,1,2]
20+
* Output: [1,2]
2721
*/
2822

2923
#include "list.h"

src/4_Lists/delete_node.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
/*
22
* Task: Remove the first occurrence of a value from a singly linked list.
33
*
4-
* REMOVE ELEMENT FROM A LINKED LIST
4+
* REMOVE LINKED LIST ELEMENT
55
*
6-
* Extend a basic singly linked list (as defined in list.h) by providing a
7-
* function to remove the first occurrence of a given value from the list.
6+
* Problem:
7+
* Given the head of a singly linked list and an integer target, remove the
8+
* first node whose value equals target. If target does not exist, keep the
9+
* list unchanged.
810
*
9-
* ASCII Illustration:
11+
* Constraints:
12+
* - 0 <= n <= 10^5
13+
* - Node values and target are 32-bit signed integers.
1014
*
11-
* Before removal: 1 -> 2 -> 3 -> 4 -> 5
12-
* remove(3)
13-
* After removal: 1 -> 2 -> 4 -> 5
15+
* Example 1:
16+
* Input: head = [1,2,3,4,5], target = 3
17+
* Output: [1,2,4,5]
1418
*
15-
* Example:
16-
* Input: List = [1, 2, 3, 4, 5], remove(3)
17-
* Output: List = [1, 2, 4, 5]
18-
*
19-
* Edge Cases:
20-
* - Removing the head element.
21-
* - Removing the tail element.
22-
* - Removing from a list with a single element.
19+
* Example 2:
20+
* Input: head = [1], target = 1
21+
* Output: []
2322
*/
2423

2524
#include "list.h"

src/4_Lists/kth_node.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
/*
22
* Task: Find the kth element from the tail of a singly linked list.
33
*
4-
* FIND KTH TO TAIL ELEMENT IN A LINKED LIST
4+
* KTH NODE FROM END OF LIST
55
*
6-
* Find the kth element from the tail (0-indexed) of a singly linked list.
7-
* Three solutions are provided:
6+
* Problem:
7+
* Given the head of a singly linked list and an integer k (0-indexed from the
8+
* tail), return the value of the kth node from the end of the list.
89
*
9-
* ASCII Illustration:
10+
* Constraints:
11+
* - 1 <= n <= 10^5
12+
* - 0 <= k < n
13+
* - Node values are 32-bit signed integers.
1014
*
11-
* Linked List: 1 -> 2 -> 3 -> 4 -> 5
15+
* Example 1:
16+
* Input: head = [1,2,3,4,5], k = 0
17+
* Output: 5
1218
*
13-
* For k = 0 (0th from tail), the answer is 5.
14-
* For k = 4 (4th from tail), the answer is 1.
15-
*
16-
* Example Input/Output:
17-
* Input: List = 1 -> 2 -> 3 -> 4 -> 5, k = 1
19+
* Example 2:
20+
* Input: head = [1,2,3,4,5], k = 1
1821
* Output: 4
19-
*
20-
* Explanation:
21-
* For k=1, the kth element from the tail is the second-to-last element in the
22-
* list.
2322
*/
2423

2524
#include "list.h"

src/4_Lists/loops.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
/*
22
* Task: Detect whether a linked list contains a loop.
33
*
4-
* DETECT LOOP IN A LINKED LIST (WITH LOOPS)
4+
* LINKED LIST CYCLE
55
*
6-
* Define a linked list class (ListWithLoops) that supports creating loops in
7-
* the list by connecting nodes arbitrarily. Memory management is handled by
8-
* storing all allocated nodes in a vector so that they can be safely deleted
9-
* even if a loop exists.
6+
* Problem:
7+
* Given the head of a linked list, determine whether it contains a cycle. A
8+
* cycle exists if some node can be reached again by continuously following
9+
* next pointers.
1010
*
11-
* ASCII Illustration:
11+
* Constraints:
12+
* - 0 <= n <= 10^5
13+
* - Node values are 32-bit signed integers.
14+
* - `next` may point to any node in the list or null.
1215
*
13-
* Without Loop:
14-
* 1 -> nullptr
16+
* Example 1:
17+
* Input: head = [1,2,3,4,5], tail connects to index 2
18+
* Output: true
1519
*
16-
* With Loop:
17-
* 1 -> 2 -> 3 -> 4 -> 5
18-
* ^ |
19-
* |_________|
20-
*
21-
* Example:
22-
* Input: List = [1, 2, 3, 4, 5] with connection from node 5 to node 3.
23-
* Output: hasLoop() returns true.
24-
*
25-
* Edge Cases:
26-
* - A single node with no loop.
27-
* - A node connected to itself.
20+
* Example 2:
21+
* Input: head = [1], tail connects to null
22+
* Output: false
2823
*/
2924

3025
#include <algorithm>

src/4_Lists/merge.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
/*
22
* Task: Merge two sorted linked lists into one sorted list.
33
*
4-
* MERGE TWO SORTED LINKED LISTS
4+
* MERGE TWO SORTED LISTS
55
*
6-
* ASCII Illustration:
6+
* Problem:
7+
* Given the heads of two sorted linked lists, merge them into one sorted list
8+
* containing all nodes from both lists.
79
*
8-
* List1: 1 -> 3 -> 5
9-
* List2: 2 -> 4 -> 6
10+
* Constraints:
11+
* - 0 <= n, m <= 10^5
12+
* - -10^9 <= Node.val <= 10^9
13+
* - Both input lists are sorted in non-decreasing order.
1014
*
11-
* Merged: 1 -> 2 -> 3 -> 4 -> 5 -> 6
15+
* Example 1:
16+
* Input: list1 = [1,3,5], list2 = [2,4,6]
17+
* Output: [1,2,3,4,5,6]
1218
*
13-
* Example:
14-
* Input: list1 = [1, 3, 5], list2 = [2, 4, 6]
15-
* Output: [1, 2, 3, 4, 5, 6]
16-
*
17-
* Edge Cases:
18-
* - One or both lists are empty.
19-
* - Lists of different lengths.
19+
* Example 2:
20+
* Input: list1 = [1,2,3], list2 = []
21+
* Output: [1,2,3]
2022
*/
2123

2224
#include "list.h"

src/4_Lists/print_reversely.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
/*
22
* Task: Print the elements of a linked list in reverse order.
33
*
4-
* PRINT LINKED LIST IN REVERSE ORDER
4+
* PRINT LINKED LIST IN REVERSE
55
*
6-
* Demonstrate two approaches to print the elements of a linked list in reverse
7-
* order.
6+
* Problem:
7+
* Given the head of a singly linked list, output the node values from tail to
8+
* head. This file demonstrates both iterative (stack) and recursive solutions.
89
*
9-
* ASCII Illustration:
10+
* Constraints:
11+
* - 0 <= n <= 10^5
12+
* - Node values are 32-bit signed integers.
1013
*
11-
* List: 1 -> 2 -> 3
14+
* Example 1:
15+
* Input: head = [1,2,3]
16+
* Output: [3,2,1]
1217
*
13-
* Reverse Output:
14-
* 3
15-
* 2
16-
* 1
17-
*
18-
* Example:
19-
* Input: List = [1, 2, 3]
20-
* Output:
21-
* (Iterative)
22-
* 3
23-
* 2
24-
* 1
25-
*
26-
* (Recursive)
27-
* 3
28-
* 2
29-
* 1
18+
* Example 2:
19+
* Input: head = []
20+
* Output: []
3021
*/
3122

3223
#include "list.h"

src/4_Lists/reverse_list.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
/*
22
* Task: Reverse a singly linked list in-place using pointer re-linking.
33
*
4-
* REVERSE A LINKED LIST USING UNIQUE_PTR
4+
* REVERSE LINKED LIST
55
*
6-
* Define a class ListWithReversion that extends a basic singly linked list
7-
* (from list.h) by providing a function to reverse the list. The reversal is
8-
* done by re-linking the nodes, which are managed by std::unique_ptr, without
9-
* creating new nodes.
6+
* Problem:
7+
* Given the head of a singly linked list, reverse the list in-place and return
8+
* the reversed list.
109
*
11-
* ASCII Illustration:
10+
* Constraints:
11+
* - 0 <= n <= 10^5
12+
* - Node values are 32-bit signed integers.
1213
*
13-
* Original List: 1 -> 2 -> 3 -> 4 -> 5
14-
* |
15-
* Reverse
16-
* V
17-
* Reversed List: 5 -> 4 -> 3 -> 2 -> 1
14+
* Example 1:
15+
* Input: head = [1,2,3,4,5]
16+
* Output: [5,4,3,2,1]
1817
*
19-
* Example:
20-
* Input: List = [1, 2, 3, 4, 5]
21-
* Output: List = [5, 4, 3, 2, 1]
22-
*
23-
* Edge Cases:
24-
* - An empty list remains empty.
25-
* - A list with a single element remains unchanged.
18+
* Example 2:
19+
* Input: head = []
20+
* Output: []
2621
*/
2722

2823
#include "list.h"

src/4_Lists/sort.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
/*
2+
* Task: Sort a singly linked list in non-decreasing order.
3+
*
4+
* SORT LINKED LIST (INSERTION SORT)
5+
*
6+
* Problem:
7+
* Given the head of a singly linked list, sort the list in ascending order.
8+
* This implementation performs insertion sort by re-linking nodes.
9+
*
10+
* Constraints:
11+
* - 0 <= n <= 10^5
12+
* - -10^9 <= Node.val <= 10^9
13+
*
14+
* Example 1:
15+
* Input: head = [2,4,5,1,3]
16+
* Output: [1,2,3,4,5]
17+
*
18+
* Example 2:
19+
* Input: head = [5,4,3,2,1]
20+
* Output: [1,2,3,4,5]
21+
*/
22+
123
#include "list.h"
224
#include <iostream>
325
#include <memory>

0 commit comments

Comments
 (0)