Files
hello-algo/ru/codes/python/modules/list_node.py
T
Yudong JinandClaude Opus 5.5 ded7ddc635 Revert the runnable-snippets batches to the #2033 state (#2038)
Reverts #2034, #2035, #2036 and #2037. The work will return in a single
reviewed PR that is completed before it merges. The tree equals 8c96e2f30
(#2033).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-16 06:45:32 +08:00

33 lines
889 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