forked from prabhupant/python-ds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete.py
More file actions
38 lines (28 loc) · 697 Bytes
/
delete.py
File metadata and controls
38 lines (28 loc) · 697 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
class Node():
def __init__(self, val):
self.val = val
self.next = None
def delete(head, val):
if head == None:
return "List is empty"
curr = head
prev = None
while curr.val != val:
if curr.next == head:
return "Val not in list"
prev = curr
curr = curr.next
if curr.next == head:
head = None
return "Deleted"
if curr == head:
prev = head
while prev.next != head:
prev = prev.next
head = curr.next
prev.next = head
elif curr.next == head:
prev.next = head
else:
prev.next = curr.next
return "Deleted"