Files
hello-algo/ko/codes/ruby/utils/list_node.rb
T
Yudong Jin 2f561d3d51 feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition

* Use UTF-8 for Python CI output on Windows

* Remove print-book promotion from the Korean site

* fix(ko): preserve emphasis before Korean particles
2026-10-29 03:46:43 +08:00

39 lines
744 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