-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.py
More file actions
74 lines (64 loc) · 2.45 KB
/
Copy pathlinked_list.py
File metadata and controls
74 lines (64 loc) · 2.45 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
67
68
69
70
71
72
73
74
from node import Node
class LinkedList:
def __init__(self):
self.head = None
def insert_node(self, value):
"""Insert a new node with the given value at the end of the linked list."""
new_node = Node(value)
if self.head is None:
self.head = new_node
elif value <= self.head.value:
new_node.next_node = self.head
self.head = new_node
# defining insertion of nodeein the middle of the linked list
else:
previous = self.head
runner = self.head.next_node
while runner is not None and (runner.value < value):
previous = runner
runner = runner.next_node
new_node.next_node = runner
previous.next_node = new_node
def print_list_items(self):
if self.head is None:
print("The list is empty.")
else:
current = self.head
while current is not None:
print(current.value, end=" -> ")
current = current.next_node
print("None")
def count_no_of_nodes(self):
"""Count the number of nodes in the linked list."""
count = 0
current = self.head
while current is not None:
count += 1
current = current.next_node
return count
# there is also a recurcive way to count the number of nodes in the linked list
def find_node(self, value):
"""Find a node with the given value in the linked list."""
current = self.head
while current is not None:
if current.value == value:
print("Node found with value!")
return current
current = current.next_node
return None
def delete_node(self, value):
"""Delete a node with the given value from the linked list."""
if self.head is None:
print("The list is empty.")
return
# If the node to be deleted is the head node
if self.head.value == value:
self.head = self.head.next_node
return
current = self.head
while current.next_node is not None:
if current.next_node.value == value:
current.next_node = current.next_node.next_node
return
current = current.next_node
print(f"Node with value {value} not found.")