How to delete last node from singly linked list in python? [Solved] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to delete last node from singly linked list in python? [Solved]

https://code.sololearn.com/cAMyq35Tg01A/?ref=app Edit: just noticed that i was assigning none to a new variable n and i thought i was setting the address to None with the following code,lol ! while n.next: n=n.nexr n=None

15th Jul 2021, 10:49 AM
Abhay
Abhay - avatar
3 Answers
+ 3
Traverse until next of next is none. Delete next def delete_from_last(self): n=self.head while n.next.next: n=n.next n.next = None additionally check if head has next(otherwise head is last)
15th Jul 2021, 11:24 AM
Oma Falk
Oma Falk - avatar
+ 1
Oma Falk thanks a lot!
15th Jul 2021, 12:08 PM
Abhay
Abhay - avatar
+ 1
Abhay you are welcome. you helped me so often. it was up to me😉
15th Jul 2021, 12:29 PM
Oma Falk
Oma Falk - avatar