-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedList.py
135 lines (114 loc) · 3.89 KB
/
LinkedList.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
class Node:
def __init__(self, value=None) -> None:
self.value = value
self.next = None
class SLinkedList:
def __init__(self) -> None:
self.head = None
self.tail = None
def __iter__(self):
node = self.head
while node:
yield node
node = node.next
def __str__(self) -> str:
values = [str(x.value) for x in self]
return ' -> '.join(values)
def __len__(self):
result = 0
node = self.head
while node:
result += 1
node = node.next
return result
# insert in Linked List
def insert_element(self, value, location):
newNode = Node(value)
if self.head is None:
self.head = newNode
self.tail = newNode
else:
# insert at first : Time: O(1)
if location == 0:
newNode.next = self.head
self.head = newNode
# insert at last: Time: O(1)
elif location == -1:
newNode.next = None
self.tail.next = newNode
self.tail = newNode
# insert at nth postion: Time: O(n)
else:
tempNode = self.head
index = 0
while index < location - 1:
tempNode = tempNode.next
index += 1
nextNode = tempNode.next
tempNode.next = newNode
newNode.next = nextNode
def delete_element(self, location):
if self.head is None:
print("Linked List does not exist")
else:
if location == 0:
# if have only one node
if self.head == self.tail:
self.head = None
self.tail = None
else:
temp = self.head
self.head = temp.next
temp = None
# delete from last position : Time: O(n)
elif location == -1:
# #1. if we have only one node
if(self.head == self.tail):
self.head = None
self.tail = None
else:
# 2. Else, traverse to the second last
# element of the list
tempNode = self.head
while(tempNode is not None):
if tempNode.next == self.tail:
break
tempNode = tempNode.next
# 3. Change the next of the second
# last node to null and delete the
# last node
tempNode.next = None
self.tail = tempNode
else:
tempNode = self.head
index = 0
while index < location - 1:
tempNode = tempNode.next
index += 1
delNode = tempNode.next
tempNode.next = delNode.next
# Delete whole SLL
# Time: O(1) ; Space: O(1)
def deleteAllSLL(self):
# Idea is to make reference to head and tail to null
# Garbage collector then destroy all node
if self.head is None:
print("The SLL does not exist")
else:
self.head = None
self.tail = None
singlyLinkedList = SLinkedList()
singlyLinkedList.insert_element(0, 0)
singlyLinkedList.insert_element(1, 0)
singlyLinkedList.insert_element(2, 0)
singlyLinkedList.insert_element(10, -1)
singlyLinkedList.insert_element(15, -1)
singlyLinkedList.insert_element(20, 3)
singlyLinkedList.insert_element(25, 4)
print(singlyLinkedList)
singlyLinkedList.delete_element(4)
singlyLinkedList.delete_element(0)
singlyLinkedList.delete_element(-1)
print(singlyLinkedList)
singlyLinkedList.deleteAllSLL()
print(singlyLinkedList)