mirror of
https://github.com/krahets/hello-algo.git
synced 2026-12-04 21:10:58 +08:00
Batch 3a of the runnable-snippets plan. heap_operations now cites the heap demos in Python, C++, Java, C#, Go, Swift, Rust and Kotlin; the demos call the library directly instead of push/pop helpers and print each popped value. list_node_class cites each language's shared ListNode (Swift stays literal) and doubly_list_node_class cites linkedlist_deque (Go stays literal). Node comments adopt reviewed source wording, Rust and Swift get import regions, the Ruby typo 前躯 becomes 前驱, and the TypeScript doubly node fields become nullable. en, ja, ko and zh-hant are regenerated; ru is synced by hand. Co-Authored-By: Claude Opus 5.5 <[email protected]>
41 lines
826 B
Ruby
41 lines
826 B
Ruby
=begin
|
|
File: list_node.rb
|
|
Created Time: 2024-03-18
|
|
Author: Xuan Khoa Tu Nguyen ([email protected])
|
|
=end
|
|
|
|
# [region]{list_node}
|
|
### 連結リストのノードクラス ###
|
|
class ListNode
|
|
attr_accessor :val # ノードの値
|
|
attr_accessor :next # 次のノードへの参照
|
|
|
|
def initialize(val=0, next_node=nil)
|
|
@val = val
|
|
@next = next_node
|
|
end
|
|
end
|
|
# [endregion]{list_node}
|
|
|
|
### リストをデシリアライズして連結リストにする ###
|
|
def arr_to_linked_list(arr)
|
|
head = current = ListNode.new(arr[0])
|
|
|
|
for i in 1...arr.length
|
|
current.next = ListNode.new(arr[i])
|
|
current = current.next
|
|
end
|
|
|
|
head
|
|
end
|
|
|
|
### 連結リストをシリアライズしてリストにする ###
|
|
def linked_list_to_arr(head)
|
|
arr = []
|
|
|
|
while head
|
|
arr << head.val
|
|
head = head.next
|
|
end
|
|
end
|