Ref: 2 step by step diagrams added.
This commit is contained in:
58
analysis/04-reverse-linked-list/diagrams/README.md
Normal file
58
analysis/04-reverse-linked-list/diagrams/README.md
Normal file
@@ -0,0 +1,58 @@
|
||||
# Reverse Linked List — PlantUML Diagrams
|
||||
|
||||
This directory contains PlantUML diagrams for the three-pointer linked list reversal algorithm.
|
||||
|
||||
The diagrams use a small list:
|
||||
|
||||
```text
|
||||
1 -> 2 -> 3 -> null
|
||||
```
|
||||
|
||||
and show how it becomes:
|
||||
|
||||
```text
|
||||
3 -> 2 -> 1 -> null
|
||||
```
|
||||
|
||||
## Files
|
||||
|
||||
- `00_initial_state.puml` — initial state before the loop
|
||||
- `01_save_next.puml` — saving `next = current->next`
|
||||
- `02_reverse_current_link.puml` — reversing `current->next`
|
||||
- `03_move_pointers.puml` — moving `previous` and `current`
|
||||
- `04_second_iteration.puml` — state after the second node is processed
|
||||
- `05_final_state.puml` — final state after the loop
|
||||
|
||||
## Generate PNG Files
|
||||
|
||||
```sh
|
||||
plantuml diagrams/*.puml
|
||||
```
|
||||
|
||||
## Generate SVG Files
|
||||
|
||||
```sh
|
||||
plantuml -tsvg diagrams/*.puml
|
||||
```
|
||||
|
||||
## Core Idea
|
||||
|
||||
During the loop, the list is logically split into two parts:
|
||||
|
||||
- `previous` points to the already reversed part
|
||||
- `current` points to the node currently being processed
|
||||
- `next` temporarily preserves access to the remaining original list
|
||||
|
||||
The key operation is:
|
||||
|
||||
```cpp
|
||||
current->next = previous;
|
||||
```
|
||||
|
||||
But this is only safe after saving:
|
||||
|
||||
```cpp
|
||||
Node* next = current->next;
|
||||
```
|
||||
|
||||
Otherwise the remaining part of the original list would be lost.
|
||||
Reference in New Issue
Block a user