Reputation: 93
I am implementing LinkedList in Python 3.6.5. I am stuck with deleting a node from the end:
Here's the code:
class Node(object):
def __init__(self, data):
self.data = data
self.nextNode = None
class LinkedList(object):
def __init__(self):
self.head = None
self.counter = 0
def print_list(self):
current = self.head
while current is not None:
print(current.data)
current = current.nextNode
def size(self):
print(self.counter)
def insert_end(self,data):
newNode = Node(data)
current = self.head
if self.head is None:
self.head = newNode
self.counter+=1
else:
while current.nextNode is not None:
current = current.nextNode
current.nextNode = newNode
self.counter+=1
def insert_end(self,data):
newNode = Node(data)
current = self.head
if self.head is None:
self.head = newNode
self.counter+=1
else:
while current.nextNode is not None:
current = current.nextNode
current.nextNode = newNode
self.counter+=1
def insert_beg(self,data):
newNode = Node(data)
if self.head is None:
self.head = newNode
self.counter+=1
else:
newNode.nextNode = self.head
self.head = newNode
self.counter+=1
def remove_beg(self):
if self.head is None:
print("Linked List is already empty")
else:
currentNode = self.head
self.head = self.head.nextNode
self.counter-=1
del currentNode
def remove_end(self):
if self.head is None:
print("Linked List is already empty")
else:
current_head = self.head
while current_head.nextNode is not None:
current_head = current_head.nextNode
current_node = current_head
current_head.nextNode = None
del current_node.nextNode
self.counter-=1
linkedlist = LinkedList()
linkedlist.remove_beg()
linkedlist.insert_end(1)
linkedlist.insert_end(2)
linkedlist.insert_end(3)
linkedlist.insert_end(4)
print("printing list")
linkedlist.print_list()
linkedlist.insert_beg(22)
print("printing list after addition of 22 in the beginning")
linkedlist.print_list()
linkedlist.remove_beg()
print("printing list")
linkedlist.print_list()
print("printing size")
linkedlist.size()
linkedlist.insert_end(55)
linkedlist.print_list()
linkedlist.remove_end()
linkedlist.print_list()
Everything works fine except removing the node from the end.
Output:
Traceback (most recent call last):
File "intermediate.py", line 84, in
linkedlist.remove_end()
File "intermediate.py", line 61, in remove_end
current_head.nextNode = None
AttributeError: 'NoneType' object has no attribute 'nextNode'
Upvotes: 1
Views: 121
Reputation: 1654
To delete a node from the last, we need to create a link between the second-last node and None
def remove_end(self):
if self.head is None:
print('List is empty!')
else:
current_head = self.head
current_node = None
while current_head.nextNode is not None:
previous = current_head
current_head = current_head.nextNode
previous.nextNode = None
del current_head
Upvotes: 1