Ref: 2 step by step diagrams added.
This commit is contained in:
BIN
analysis/04-reverse-linked-list/diagrams/00_initial_state.png
Normal file
BIN
analysis/04-reverse-linked-list/diagrams/00_initial_state.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
@@ -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
|
||||||
BIN
analysis/04-reverse-linked-list/diagrams/01_save_next.png
Normal file
BIN
analysis/04-reverse-linked-list/diagrams/01_save_next.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 28 KiB |
38
analysis/04-reverse-linked-list/diagrams/01_save_next.puml
Normal file
38
analysis/04-reverse-linked-list/diagrams/01_save_next.puml
Normal 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 |
@@ -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
|
||||||
BIN
analysis/04-reverse-linked-list/diagrams/03_move_pointers.png
Normal file
BIN
analysis/04-reverse-linked-list/diagrams/03_move_pointers.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
@@ -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
|
||||||
BIN
analysis/04-reverse-linked-list/diagrams/04_second_iteration.png
Normal file
BIN
analysis/04-reverse-linked-list/diagrams/04_second_iteration.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 25 KiB |
@@ -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
|
||||||
BIN
analysis/04-reverse-linked-list/diagrams/05_final_state.png
Normal file
BIN
analysis/04-reverse-linked-list/diagrams/05_final_state.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 23 KiB |
35
analysis/04-reverse-linked-list/diagrams/05_final_state.puml
Normal file
35
analysis/04-reverse-linked-list/diagrams/05_final_state.puml
Normal 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
|
||||||
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