-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked-list.py
More file actions
52 lines (45 loc) · 1.33 KB
/
linked-list.py
File metadata and controls
52 lines (45 loc) · 1.33 KB
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
# Node of a singly linked list
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Creation and Traversal of Singly Linked List:
class LinkedList:
def __init__(self):
self.head = None
def printList(self):
temp = self.head
while temp:
print(temp.data)
temp = temp.next
# Insertion of Node in Linked List:
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
self.head = new_node
# Insertion of Node in Linked List:
def insertAfter(self, prev_node, new_data):
if prev_node is None:
print("The given previous node must inLinkedList.")
return
new_node = Node(new_data)
new_node.next = prev_node.next
prev_node.next = new_node
def append(self, new_data):
new_node = Node(new_data)
if self.head is None:
self.head = new_node
return
last = self.head
while(last.next):
last = last.next
last.next = new_node
if __name__=='__main__':
llist = LinkedList()
llist.append(6)
llist.push(7)
llist.push(1)
llist.append(4)
llist.insertAfter(llist.head.next, 8)
print('Created Linked list is: ')
llist.printList()