-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathlinked_list_utils.py
More file actions
44 lines (31 loc) · 805 Bytes
/
linked_list_utils.py
File metadata and controls
44 lines (31 loc) · 805 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Node:
val: int
next = None
def __init__(self, val, next_node=None):
self.val = val
self. next = next_node
def __str__(self):
return f"({self.val}) -> {self.next}"
def link_list_of_list(arr) -> Node:
head = Node(arr[0])
now_elem = head
for x in arr[1:]:
now_elem.next = Node(x)
now_elem = now_elem.next
return head
def llol(arr):
"""Shorthand for (make) linked-list of list
"""
return link_list_of_list(arr)
def lllen(head: Node) -> int:
return len_of_ll(head)
def len_of_ll(head: Node) -> int:
count = 0
elem_now = head
while elem_now:
count += 1
elem_now = elem_now.next
return count
if __name__ == "__main__":
ex1 = [1, 2, 3]
print(link_list_of_list(ex1))