forked from devAmoghS/Python-Interview-Problems-for-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list_data_structure.py
84 lines (71 loc) · 2.36 KB
/
linked_list_data_structure.py
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
75
76
77
78
79
80
81
82
83
84
class Node:
def __init__(self, data=None):
self.data = data
self.next_node = None
class LinkedList:
def __init__(self, head=None):
self.head = head
def isEmpty(self):
return self.head == None
def insert(self, data):
# create a temp node
temp = Node(data=data)
# point new node to head
temp.next_node = self.head
# set the head as new node
self.head = temp
def insert_after(self, prev, data):
if prev is None:
raise ValueError("Given node is not found...")
return prev
# create a temp node
temp = Node(data=data)
# set next node of temp to the next node of previous
temp.next_node = prev.next_node
# set next node of previous to point temp
prev.next_node = temp
def size(self):
# start with the head
current = self.head
count = 0
# loop unless current is not None
while current:
count += 1
current = current.next_node
return count
def search(self, data):
# start with the head
current = self.head
found = False
# loop unless current is not None
while current and not found:
# if found, change flag and return data
if current.data == data:
found = True
else:
# change current to next node
current = current.next_node
if current is None:
# raise Exception if not found
raise ValueError("Data is not in the list")
return current
def delete(self, data):
# start with the head
current = self.head
previous = None
found = False
# loop unless current is not None
while current and not found:
# if found, change flag
if current.getData() == data:
found = True
else:
previous = current
current = current.next_node
if current is None:
# raise Exception if not found
raise ValueError("Data is not in the list")
if previous is None:
self.head = current.next_node
else:
previous.next_node = current.next_node