
-----------------------------------
jamonathin
Wed Nov 07, 2007 10:37 am

Linked Lists
-----------------------------------
Has anyone here dealt with linked lists in java?  I've figured everything out except on how to completely delete an element.  I can do all the changing values, but as far as erasing the last node, no such luck.

Simply doing:

currentNode = new intNode (0, null);
or even
currentNode = null;

doesn't seem to work, and ideas?

-----------------------------------
HeavenAgain
Wed Nov 07, 2007 10:45 am

RE:Linked Lists
-----------------------------------
isnt there a method called remove?

-----------------------------------
Tony
Wed Nov 07, 2007 11:50 am

RE:Linked Lists
-----------------------------------
to remove an object in Java (lets say a node of a linked list), you just have to make sure that nothing points to it -- Java's garbage collector will get to it eventually.

So to remove "current" node, you set "previous" node to point to null. Something like

prev.setNext(null);

doing anything along the lines of

currentNode = null; 

doesn't really do anything, since currentNode is most likely just a copy of a pointer -- changing this value will not change the actual object.

-----------------------------------
jamonathin
Wed Nov 07, 2007 1:28 pm

Re: Linked Lists
-----------------------------------
Excellent, thanks Tony.  I dont have time at the moment, but i'll implement this later :)
Im not sure about a remove method 'heavenagain', but i think Tony's idea will cover what i need.
