-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.py
More file actions
41 lines (35 loc) · 950 Bytes
/
linked_list.py
File metadata and controls
41 lines (35 loc) · 950 Bytes
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
class linknode:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.start = None
self.end = None
def insert_at_head(self, item):
node = linknode(item)
node.next = self.start
self.start = node
self.end = node.next
def insert_at_end(self, item):
node = linknode(item)
if(self.start==None):
node.next = self.start
self.start = node
self.end = node
else:
self.end.next = node
node.next = None
self.end = node
def printList(self):
node = self.start
while node:
print(node.data)
node = node.next
arr = LinkedList()
arr.insert_at_end(5)
arr.insert_at_head(20)
arr.insert_at_end(8)
arr.insert_at_end(2)
arr.insert_at_head(16)
arr.printList()