mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-06 05:48:58 +08:00
translation: Add Python and Java code for EN version (#1345)
* Add the intial translation of code of all the languages * test * revert * Remove * Add Python and Java code for EN version
This commit is contained in:
32
en/codes/python/modules/list_node.py
Normal file
32
en/codes/python/modules/list_node.py
Normal file
@ -0,0 +1,32 @@
|
||||
"""
|
||||
File: list_node.py
|
||||
Created Time: 2021-12-11
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
class ListNode:
|
||||
"""LinkedList node class"""
|
||||
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # Node value
|
||||
self.next: ListNode | None = None # Reference to the next node
|
||||
|
||||
|
||||
def list_to_linked_list(arr: list[int]) -> ListNode | None:
|
||||
"""Deserialize a list into a linked list"""
|
||||
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]:
|
||||
"""Serialize a linked list into a list"""
|
||||
arr: list[int] = []
|
||||
while head:
|
||||
arr.append(head.val)
|
||||
head = head.next
|
||||
return arr
|
Reference in New Issue
Block a user