mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-21 23:16:41 +08:00
refactor: Follow the PEP 585 Typing standard (#439)
* Follow the PEP 585 Typing standard * Update list.py
This commit is contained in:
@ -4,15 +4,13 @@ Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
from typing import List, Optional
|
||||
|
||||
class ListNode:
|
||||
""" Definition for a singly-linked list node """
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 结点值
|
||||
self.next: Optional[ListNode] = None # 后继结点引用
|
||||
self.next: ListNode | None = None # 后继结点引用
|
||||
|
||||
def list_to_linked_list(arr: List[int]) -> Optional[ListNode]:
|
||||
def list_to_linked_list(arr: list[int]) -> ListNode | None:
|
||||
""" Generate a linked list with a list """
|
||||
dum = head = ListNode(0)
|
||||
for a in arr:
|
||||
@ -21,15 +19,15 @@ def list_to_linked_list(arr: List[int]) -> Optional[ListNode]:
|
||||
head = head.next
|
||||
return dum.next
|
||||
|
||||
def linked_list_to_list(head: Optional[ListNode]) -> List[int]:
|
||||
def linked_list_to_list(head: ListNode | None) -> list[int]:
|
||||
""" Serialize a linked list into an array """
|
||||
arr: List[int] = []
|
||||
arr: list[int] = []
|
||||
while head:
|
||||
arr.append(head.val)
|
||||
head = head.next
|
||||
return arr
|
||||
|
||||
def get_list_node(head: Optional[ListNode], val: int) -> Optional[ListNode]:
|
||||
def get_list_node(head: ListNode | None, val: int) -> ListNode | None:
|
||||
""" Get a list node with specific value from a linked list """
|
||||
while head and head.val != val:
|
||||
head = head.next
|
||||
|
Reference in New Issue
Block a user