docs(docs): add Approach and Explanation sections with mermaid diagrams to problem pages

This commit is contained in:
Prad Nukala committed 2026-09-01 14:21:54 -04:00
1 parent e59d78e571
commit a86d72c7db
43 files changed
+812

No files matched your search

@@ -29,6 +29,19 @@ A linked list can be reversed either iteratively or recursively. Could you imple
- The number of nodes in the list is the range [0, 5000].
- `-5000 <= Node.val <= 5000`
## Approach
```mermaid
flowchart TD
S(["reverseList(head)"]) --> I["prev = None, curr = head"]
I --> W{"curr?"}
W -- no --> E(["return prev — the old tail is the new head"])
W -- yes --> A["next_ = curr.next — save it before overwriting"]
A --> B["curr.next = prev — flip the link"]
B --> C["prev = curr; curr = next_ — step forward"]
C --> W
```
## Solution
```py
@@ -48,3 +61,7 @@ class Solution:
curr = next_
return prev
```
## Explanation
<YouTube id="G0_I-ZF0S38" title="Reverse Linked List - Iterative AND Recursive - Leetcode 206 - Python" />
@@ -26,6 +26,23 @@ sidebar:
- `-100 <= Node.val <= 100`
- Both list1 and list2 are sorted in non-decreasing order.
## Approach
```mermaid
flowchart TD
S(["mergeTwoLists(list1, list2)"]) --> G1{"list1 empty?"}
G1 -- yes --> X1(["return list2"])
G1 -- no --> G2{"list2 empty?"}
G2 -- yes --> X2(["return list1"])
G2 -- no --> H["head = the smaller first node; advance that list"]
H --> C["current = head"]
C --> W{"list1 and list2 both non-empty?"}
W -- yes --> P["current.next = the smaller node; advance that list; current = current.next"]
P --> W
W -- no --> T["current.next = list1 or list2 — append the leftover tail"]
T --> E(["return head"])
```
## Solution
```py
@@ -64,3 +81,7 @@ class Solution:
current.next = list1 or list2
return head
```
## Explanation
<YouTube id="XIdigk956u0" title="Merge Two Sorted Lists - Leetcode 21 - Python" />