-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.py
More file actions
67 lines (58 loc) · 1.61 KB
/
linked_list.py
File metadata and controls
67 lines (58 loc) · 1.61 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def print_list(self) -> str:
"""
Print the list information
"""
ll_string = ""
current_node = self.head
while current_node:
ll_string += f"{str(current_node.data)}->\t"
current_node = current_node.next
ll_string += 'None'
print(ll_string)
def append(self, data):
"""
Add the node in the last position
"""
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
last_node = self.head
while last_node.next:
last_node = last_node.next
last_node.next = new_node
def prepend(self, data):
"""
Add the node as the head
"""
new_node = Node(data)
new_node.next = self.head
self.head = new_node
def to_list(self):
"""
Return a list with linked list nodes
"""
arr = []
if self.head:
current_node = self.head
while current_node:
arr.append(current_node.data)
current_node = current_node.next
return arr
def get_user_by_id(self, user_id):
"""
Find a user by id
"""
current_node = self.head
while current_node:
if current_node.data['id'] is user_id:
return current_node.data
current_node = current_node.next
return None