61 lines
1.3 KiB
Python
61 lines
1.3 KiB
Python
def ll_insert(head: dict | None, name: str, phone: str) -> dict:
|
|
if head is None:
|
|
return {'name': name, 'phone': phone, 'next': None}
|
|
|
|
current = head
|
|
|
|
while current is not None:
|
|
if current['name'] == name:
|
|
current['phone'] = phone
|
|
return head
|
|
|
|
if current['next'] is None:
|
|
break
|
|
|
|
current = current['next']
|
|
|
|
current['next'] = {'name': name, 'phone': phone, 'next': None}
|
|
return head
|
|
|
|
|
|
def ll_find(head: dict | None, name: str) -> str | None:
|
|
current = head
|
|
|
|
while current is not None:
|
|
if current['name'] == name:
|
|
return current['phone']
|
|
current = current['next']
|
|
|
|
return None
|
|
|
|
|
|
def ll_delete(head: dict | None, name: str) -> dict | None:
|
|
if head is None:
|
|
return None
|
|
|
|
if head['name'] == name:
|
|
return head['next']
|
|
|
|
current = head
|
|
|
|
while current['next'] is not None:
|
|
if current['next']['name'] == name:
|
|
current['next'] = current['next']['next']
|
|
return head
|
|
|
|
current = current['next']
|
|
|
|
return head
|
|
|
|
|
|
def ll_list_all(head: dict | None) -> list:
|
|
records = []
|
|
current = head
|
|
|
|
while current is not None:
|
|
records.append((current['name'], current['phone']))
|
|
current = current['next']
|
|
|
|
records.sort(key=lambda item: item[0])
|
|
return records
|