-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpleNodeDeletion.py
More file actions
64 lines (48 loc) · 1.39 KB
/
simpleNodeDeletion.py
File metadata and controls
64 lines (48 loc) · 1.39 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
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 19 14:10:11 2021
@author: Utpal
"""
class Node(object):
def __init__(self, val = 0):
self.val = val
self.next = None
class LL():
def __init__(self, head):
self.head = head
def deleteNode(self, head, index):
if (head is None):
return "Error: cant delete from empty list"
i = 1 # count at current node
if (index ==1): #delete head
head = head.next
else:
prev = head
curr = head
while (curr.next is not None):
prev = curr
curr = curr.next
i+=1
if (i == index) and (curr.next is None):
prev.next = None
elif (i==index) and (curr.next is not None):
prev.next = curr.next
return head
# assume index is always less than the length of the list
head = Node(7)
head.next = Node(10)
head.next.next = Node (20)
head.next.next.next = Node(50)
# head.next.next.next.next = None
# 7 --> 10 ---> 20 ---> 50
curr = head
while (curr != None):
print (curr.val)
curr = curr.next
llclass = LL(head)
head = llclass.deleteNode(head, 1)
curr = head
while (curr != None):
print (curr.val)
curr = curr.next
head = None