mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 23:50:45 +08:00
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]>
33 lines
889 B
Python
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
|