feat(patterns): pattern taxonomy + is_optimal

This commit is contained in:
2025-09-08 16:03:14 +01:00
parent 06d99a7f04
commit fe59de3392
28 changed files with 1434 additions and 26 deletions

View File

@@ -1,6 +1,8 @@
name: LinkedList In-Place Reversal
slug: linkedlist-reversal
difficulty_level: 2
pattern_type: technique
display_order: 14
description: >
Reverse linked list nodes in-place by manipulating pointers without allocating
@@ -277,3 +279,130 @@ related_patterns:
- two-pointers
prerequisite_patterns: []
visualization_examples:
- id: reverse-linked-list
title: Reverse Entire Linked List
input:
list: [1, 2, 3, 4]
code: |
def reverse_list(head):
prev = None
curr = head
while curr:
next_node = curr.next
curr.next = prev
prev = curr
curr = next_node
return prev
steps:
- id: step-1
description: "Initialize prev=None, curr=head (node 1)."
structures:
list:
type: linkedlist
nodes:
- { value: 1, state: active }
- { value: 2, state: default }
- { value: 3, state: default }
- { value: 4, state: default }
pointers:
curr: 0
variables:
prev: "null"
codeHighlight:
startLine: 2
endLine: 3
- id: step-2
description: "Save next=2, reverse link: 1→null. Move pointers forward."
structures:
list:
type: linkedlist
nodes:
- { value: 1, state: visited, annotations: ["→null"] }
- { value: 2, state: active }
- { value: 3, state: default }
- { value: 4, state: default }
pointers:
prev: 0
curr: 1
variables:
next_node: 2
codeHighlight:
startLine: 6
endLine: 9
- id: step-3
description: "Save next=3, reverse link: 2→1. Move pointers forward."
structures:
list:
type: linkedlist
nodes:
- { value: 1, state: visited, annotations: ["→null"] }
- { value: 2, state: visited, annotations: ["→1"] }
- { value: 3, state: active }
- { value: 4, state: default }
pointers:
prev: 1
curr: 2
variables:
next_node: 3
codeHighlight:
startLine: 6
endLine: 9
- id: step-4
description: "Save next=4, reverse link: 3→2. Move pointers forward."
structures:
list:
type: linkedlist
nodes:
- { value: 1, state: visited, annotations: ["→null"] }
- { value: 2, state: visited, annotations: ["→1"] }
- { value: 3, state: visited, annotations: ["→2"] }
- { value: 4, state: active }
pointers:
prev: 2
curr: 3
variables:
next_node: 4
codeHighlight:
startLine: 6
endLine: 9
- id: step-5
description: "Save next=null, reverse link: 4→3. curr becomes null, loop ends."
structures:
list:
type: linkedlist
nodes:
- { value: 1, state: visited, annotations: ["→null"] }
- { value: 2, state: visited, annotations: ["→1"] }
- { value: 3, state: visited, annotations: ["→2"] }
- { value: 4, state: found, annotations: ["→3", "new head"] }
pointers:
prev: 3
variables:
next_node: "null"
codeHighlight:
startLine: 11
endLine: 11
- id: step-6
description: "Return prev (node 4). Reversed list: 4→3→2→1→null"
structures:
reversed:
type: linkedlist
nodes:
- { value: 4, state: found, annotations: ["head"] }
- { value: 3, state: default }
- { value: 2, state: default }
- { value: 1, state: default }
variables:
result: "4→3→2→1→null"
codeHighlight:
startLine: 11
endLine: 11