+ 1
Remove a node in LinkList (python)
I have a Linklist: a -> b -> c. I want to remove node "b". I will update link of node "a" from "b" to "c". Mean: a -> c. What about node "b"? Is it delete automatically? Do I have to update link of node "b" to Null? Thanks!
4 Respuestas
+ 10
Josh Greig
yes I was talking about C/C++ or general algorithm for it not specific to any language.
I searched for python and I think memory of b will get free automatically by garbage collector as no reference to node b will be left.
no need to worry about memory leaks, It is nice I was not knowing about it earlier.
https://stackabuse.com/basics-of-memory-management-in-python/
https://medium.com/datadriveninvestor/how-does-memory-allocation-work-in-python-and-other-languages-d2d8a9398543
+ 10
You firstly need to get to node a and store a.next which is reference to b in some temp variable.
Then temp = temp.next; for making next element to a as c.
Then you can free memory of b whose reference is in temp variable if you want.
Or directly a.next = a.next.next; if you don't want to free space taken by b node.
+ 3
No. You don't need to do anything to b after a points to c.
I assume you have a singly linked list. In other words, you have a "next" property on each node but you don't have a previous. If you had a back link or previous link you'd also need to link c to a directly too. Either way, no updates to b are needed.
+ 1
Gaurav, how would you free the memory in Python? Maybe you're thinking of c or c++.
The question mentions Python and is tagged with Python.