Files
hello-algo/ja/codes/python/modules/list_node.py
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

33 lines
829 B
Python

"""
File: list_node.py
Created Time: 2021-12-11
Author: krahets ([email protected])
"""
class ListNode:
"""連結リストのノードクラス"""
def __init__(self, val: int):
self.val: int = val # ノードの値
self.next: ListNode | None = None # 後続ノードへの参照
def list_to_linked_list(arr: list[int]) -> ListNode | None:
"""リストをデシリアライズして連結リストにする"""
dum = head = ListNode(0)
for a in arr:
node = ListNode(a)
head.next = node
head = head.next
return dum.next
def linked_list_to_list(head: ListNode | None) -> list[int]:
"""連結リストをシリアライズしてリストにする"""
arr: list[int] = []
while head:
arr.append(head.val)
head = head.next
return arr