mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-25 08:01:42 +08:00
Batch 3a of the runnable-snippets plan. heap_operations now cites the heap demos in Python, C++, Java, C#, Go, Swift, Rust and Kotlin; the demos call the library directly instead of push/pop helpers and print each popped value. list_node_class cites each language's shared ListNode (Swift stays literal) and doubly_list_node_class cites linkedlist_deque (Go stays literal). Node comments adopt reviewed source wording, Rust and Swift get import regions, the Ruby typo 前躯 becomes 前驱, and the TypeScript doubly node fields become nullable. en, ja, ko and zh-hant are regenerated; ru is synced by hand. Co-Authored-By: Claude Opus 5.5 <[email protected]>
35 lines
938 B
Python
35 lines
938 B
Python
"""
|
|
File: list_node.py
|
|
Created Time: 2021-12-11
|
|
Author: krahets ([email protected])
|
|
"""
|
|
|
|
|
|
# [region]{list_node}
|
|
class ListNode:
|
|
"""Класс узла связного списка"""
|
|
|
|
def __init__(self, val: int):
|
|
self.val: int = val # Значение узла
|
|
self.next: ListNode | None = None # Ссылка на следующий узел
|
|
# [endregion]{list_node}
|
|
|
|
|
|
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
|