-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.py
42 lines (29 loc) · 859 Bytes
/
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
class Node:
def __init__(self, dataval):
self.dataval = dataval
self.nextval = None
def __str__(self):
return self.dataval
def __next__(self):
return self.nextval.dataval
class LinkedList:
def __init__(self):
self.head = None
def listprint(self):
printval = self.head
while printval is not None:
print(printval.dataval)
printval = printval.nextval
def insert_beginning(self, new_data):
new_node = Node(new_data)
new_node.nextval = self.head
self.head = new_node
linked = LinkedList()
linked.head = Node('Monday')
n2 = Node('Tuesday')
n3 = Node('Wednesday')
linked.head.nextval = n2
n2.nextval = n3
linked.listprint()
linked.insert_beginning('Sunday')
linked.listprint()