forked from wafarifki/Hacktoberfest_2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedlist.py
48 lines (36 loc) · 1.14 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
class Node:
def __init__(self, data,next=None):
self.data= data
self.next=next
class linkedlist:
def __init__(self):
self.head=None
def insert(self, data):
newNode = Node(data)
if (self.head):
currentNode = self.head
while(currentNode.next):
currentNode = currentNode.next
currentNode.next = newNode
else:
self.head = newNode
def delete(self, data):
currentNode = self.head
while(currentNode):
if(currentNode.data==data):
self.head=currentNode.next
print("Node deleted")
return
prev = currentNode
currentNode = currentNode.next
if (currentNode.data == data):
prev.next = currentNode.next
print("Node deleted")
return
else:
print("Node not found")
def printLL(self):
currentNode = self.head
while(currentNode):
print(currentNode.data)
currentNode = currentNode.next