Files
beyond-interviews/analysis/04-reverse-linked-list/diagrams/README.md

59 lines
1.2 KiB
Markdown

# 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.