Add typing annotations to Python codes. (#411)

This commit is contained in:
Yudong Jin
2023-03-12 18:49:52 +08:00
committed by GitHub
parent 2029d2b939
commit 9151eaf533
50 changed files with 577 additions and 817 deletions

View File

@ -4,22 +4,16 @@ 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=0, next=None):
self.val = val
self.next = next
""" Definition for a singly-linked list node """
def __init__(self, val: int):
self.val: int = val # 结点值
self.next: Optional[ListNode] = None # 后继结点引用
def list_to_linked_list(arr):
"""Generate a linked list with a list
Args:
arr ([type]): [description]
Returns:
[type]: [description]
"""
def list_to_linked_list(arr: List[int]) -> Optional[ListNode]:
""" Generate a linked list with a list """
dum = head = ListNode(0)
for a in arr:
node = ListNode(a)
@ -27,31 +21,16 @@ def list_to_linked_list(arr):
head = head.next
return dum.next
def linked_list_to_list(head):
"""Serialize a linked list into an array
Args:
head ([type]): [description]
Returns:
[type]: [description]
"""
arr = []
def linked_list_to_list(head: Optional[ListNode]) -> List[int]:
""" Serialize a linked list into an array """
arr: List[int] = []
while head:
arr.append(head.val)
head = head.next
return arr
def get_list_node(head, val):
"""Get a list node with specific value from a linked list
Args:
head ([type]): [description]
val ([type]): [description]
Returns:
[type]: [description]
"""
def get_list_node(head: Optional[ListNode], val: int) -> Optional[ListNode]:
""" Get a list node with specific value from a linked list """
while head and head.val != val:
head = head.next
return head