-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.py
135 lines (111 loc) · 3.41 KB
/
linked_list.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
'''
@author: vnaazleen
Program: Singly linked list
Basic operations:
Traversing the list - Time complexity - O(n) space complexity- O(1)
Inserting an item in the list
- at the beginning => Time complexity - O(1) space complexity - O(1)
- at the end => Time complexity - O(n) space complexity - O(1)
- at random position => Time complexity - O(n) space complexity - O(1)
Deleting an item from the list
- deleting first node => Time complexity - O(1) space complexity - O(1)
- deleting last node => Time complexity - O(n) space complexity - O(1)
- deleting an intermediate node => Time complexity - O(n) space complexity - O(1)
'''
class Node:
def __init__(self, data=None, next=None):
self.data = data
self.next = next
class LinkedList:
def __init__(self):
self.head = None
def prepend(self, data):
'''Insert a element at beginning'''
node = Node(data, self.head)
self.head = node
def append(self, data):
'''Inserting an element at the end'''
if self.head is None:
self.head = Node(data)
return
node = Node(data)
itr = self.head
while itr.next:
itr = itr.next
itr.next = node
def insert(self, index : int, data : int):
'''Insert an element at given position'''
node = Node(data)
itr = self.head
while itr.next and index > 1:
itr = itr.next
index -= 1
if index == 1:
node.next = itr.next
itr.next = node
def pop(self):
'''Deletes an element at end of the linked list'''
if not self.head:
return
if not self.head.next:
self.head = None
return
itr = self.head
while itr.next.next:
itr = itr.next
itr.next = None
def pop_front(self):
'''Deletes an element at front of linked list'''
ll.pop()
if not self.head:
return
self.head = self.head.next
def pop_position(self, index : int):
'''Deletes an element at given position in linked list'''
if (index == 0):
self.pop_front()
itr = self.head
while itr.next and index > 1:
itr = itr.next
index -= 1
if index == 1 and itr.next:
itr.next = itr.next.next
def print(self):
'''Prints the Linked list'''
if self.head is None:
print("List is Empty")
return
itr = self.head
llstr = "[" + str(itr.data) + "] ->"
while itr.next.next:
itr = itr.next
llstr += "[" + str(itr.data) + "] ->"
print(llstr + "[" + str(itr.next.data) + "]")
if __name__ == '__main__':
ll = LinkedList()
ll.prepend(25)
ll.prepend(20)
ll.prepend(10)
ll.prepend(5)
ll.append(30)
ll.append(35)
ll.insert(2, 15)
print("Linked list:",end=" ")
ll.print()
print("Pop front:",end=" ")
ll.pop_front()
ll.print()
print("Pop:",end=" ")
ll.pop()
ll.print()
print("Pop index 2:",end=" ")
ll.pop_position(2)
ll.print()
"""
Ouput:
=====
Linked list: [5] ->[10] ->[15] ->[20] ->[25] ->[30] ->[35]
Pop front: [10] ->[15] ->[20] ->[25] ->[30]
Pop: [10] ->[15] ->[20] ->[25]
Pop index 2: [10] ->[15] ->[25]
"""