Files
hello-algo/ja/codes/ruby/utils/list_node.rb
T
Yudong Jin 7257641e0e Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese

* Fit Japanese homepage labels and localize heading permalinks

* Point Japanese contribution instructions to the Japanese preview

* Replace locale conditionals with multilingual UI tables
2026-10-30 02:12:26 +08:00

39 lines
779 B
Ruby

=begin
File: list_node.rb
Created Time: 2024-03-18
Author: Xuan Khoa Tu Nguyen ([email protected])
=end
### 連結リストのノードクラス ###
class ListNode
attr_accessor :val # ノードの値
attr_accessor :next # 次のノードへの参照
def initialize(val=0, next_node=nil)
@val = val
@next = next_node
end
end
### リストをデシリアライズして連結リストにする ###
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