2024-03-30 13:10:46 +08:00
|
|
|
=begin
|
|
|
|
File: list_node.rb
|
|
|
|
Created Time: 2024-03-18
|
|
|
|
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
|
|
|
|
=end
|
|
|
|
|
|
|
|
### 链表节点类 ###
|
|
|
|
class ListNode
|
|
|
|
attr_accessor :val # 节点值
|
|
|
|
attr_accessor :next # 指向下一节点的引用
|
|
|
|
|
|
|
|
def initialize(val=nil, next_node=nil)
|
|
|
|
@val = val || 0
|
|
|
|
@next = next_node
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
### 将列表反序列化为链表 ###
|
2024-03-30 13:10:46 +08:00
|
|
|
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
|
|
|
|
|
2024-03-31 03:06:41 +08:00
|
|
|
### 将链表序列化为列表 ###
|
2024-03-30 13:10:46 +08:00
|
|
|
def linked_list_to_arr(head)
|
|
|
|
arr = []
|
|
|
|
|
|
|
|
while head
|
|
|
|
arr << head.val
|
|
|
|
head = head.next
|
|
|
|
end
|
|
|
|
end
|