Ref: 2 step by step diagrams added.

This commit is contained in:
2026-06-17 17:48:09 -04:00
parent 939935c258
commit fc364dd739
13 changed files with 280 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@@ -0,0 +1,35 @@
@startuml
title Step 0 — Initial State
object "Node 1" as n1 {
value = 1
}
object "Node 2" as n2 {
value = 2
}
object "Node 3" as n3 {
value = 3
}
n1 --> n2 : next
n2 --> n3 : next
n3 --> "null" : next
object "previous" as prev
object "current" as cur
prev --> "null"
cur --> n1
note bottom
Before the loop starts:
previous = null
current = head
The original list is still intact.
end note
@enduml

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

View File

@@ -0,0 +1,38 @@
@startuml
title Step 1 — Save next
object "Node 1" as n1 {
value = 1
}
object "Node 2" as n2 {
value = 2
}
object "Node 3" as n3 {
value = 3
}
n1 --> n2 : next
n2 --> n3 : next
n3 --> "null" : next
object "previous" as prev
object "current" as cur
object "next" as nxt
prev --> "null"
cur --> n1
nxt --> n2
note right of nxt
next = current->next
We save the next node before changing
current->next.
Without this temporary pointer, the rest
of the original list would become unreachable.
end note
@enduml

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

View File

@@ -0,0 +1,38 @@
@startuml
title Step 2 — Reverse current->next
object "Node 1" as n1 {
value = 1
}
object "Node 2" as n2 {
value = 2
}
object "Node 3" as n3 {
value = 3
}
n1 --> "null" : next
n2 --> n3 : next
n3 --> "null" : next
object "previous" as prev
object "current" as cur
object "next" as nxt
prev --> "null"
cur --> n1
nxt --> n2
note right of n1
current->next = previous
Node 1 no longer points to Node 2.
It now points to the already reversed part.
At the first iteration, the reversed part is empty,
so Node 1 points to null.
end note
@enduml

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@@ -0,0 +1,39 @@
@startuml
title Step 3 — Move previous and current
object "Node 1" as n1 {
value = 1
}
object "Node 2" as n2 {
value = 2
}
object "Node 3" as n3 {
value = 3
}
n1 --> "null" : next
n2 --> n3 : next
n3 --> "null" : next
object "previous" as prev
object "current" as cur
prev --> n1
cur --> n2
note right
previous = current
current = next
The reversed part is now:
1 -> null
The remaining original part is still:
2 -> 3 -> null
end note
@enduml

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

View File

@@ -0,0 +1,37 @@
@startuml
title Step 4 — Second Iteration After Reversing Node 2
object "Node 1" as n1 {
value = 1
}
object "Node 2" as n2 {
value = 2
}
object "Node 3" as n3 {
value = 3
}
n2 --> n1 : next
n1 --> "null" : next
n3 --> "null" : next
object "previous" as prev
object "current" as cur
object "next" as nxt
prev --> n2
cur --> n3
nxt --> n3
note bottom
After processing Node 2:
2 -> 1 -> null
The reversed part grows from the front.
The remaining part starts at current.
end note
@enduml

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View File

@@ -0,0 +1,35 @@
@startuml
title Step 5 — Final State
object "Node 1" as n1 {
value = 1
}
object "Node 2" as n2 {
value = 2
}
object "Node 3" as n3 {
value = 3
}
n3 --> n2 : next
n2 --> n1 : next
n1 --> "null" : next
object "head" as head
object "previous" as prev
object "current" as cur
head --> n3
prev --> n3
cur --> "null"
note right of head
When current becomes null,
previous points to the new head.
head = previous
end note
@enduml

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