Reputation: 11
Hi Guys I am doing some testing with Java.
See the method deleteMiddleNode()
, which is supposed to, given a node, delete it in a linked list.
This all works fine except for the case when I want to delete the last node.
What I did is set the last node to null if that's the one to be deleted (see the comments). It doesn't really work, and s5
still has its value.
It does work when I just want to reset the attribute key
of s5
.
I wonder why it behaves like this.
public class Three {
static SinglyNode s5 = new SinglyNode(5);
public static void main(String[] args) {
SinglyNode s1 = new SinglyNode(1);
SinglyNode s2 = new SinglyNode(2);
SinglyNode s3 = new SinglyNode(3);
SinglyNode s4 = new SinglyNode(4);
s1.next = s2;
s2.next = s3;
s3.next = s4;
s4.next = s5;
deleteMiddleNode(s5);
SinglyNode ptr = s1;
while (ptr!= null) {
System.out.print(ptr.key+" ");
ptr = ptr.next;
}
}
// this method is wrong
public static boolean deleteMiddleNode(SinglyNode node){
if (node == null) {
return false;
}
if (node.next == null) { // last node
/* this part does work
System.out.println(node.key);
node.key = 222;
System.out.println(s5.key);
*/
node = null; // but this doesn't set the last node to null.
return true;
}
node.key = node.next.key;
node.next = node.next.next;
return true;
}
}
class SinglyNode {
int key = 0;
SinglyNode next = null;
public SinglyNode (int key){
this.key = key;
}
}
Upvotes: 0
Views: 88
Reputation: 11822
Let's say you have a list of linked nodes: A -> B -> C -> D -> E.
To remove node C from the linked list, you need to find the node that is linked to C, in this case B, and change its target node to D (what C used to be linked to).
Then, since C is not referenced by anyone, it will eventually be garbage collected.
So in your example, to remove s5, you need to set the .next attribute of s4 to null, thus indicating that the linked list ends at s4.
Since you always keep a reference to the first item in the list, if you want to remove this first item, simply replace your 'first item reference' with its .next reference.
Upvotes: 1
Reputation: 56727
As said in the comments, Java passes parameters by value, not by reference. So setting node
to null
in deleteMiddleNode
does set the local variable (the parameter variable) to null
, but not the variable you passed from the caller.
You need to set this to null
within the calling code.
Another way would be to pass both the node (node
) to be deleted and its preceding node (previous
) to the method and then set previous.next
to node.next
to delete the node from the chain (node
itself doesn't have to be set to null
, as it will be garbage collected anyway. Setting things to null
doesn't influence this). Then again you need special handling for the first node, as you lose the "entry point" into your list.
Upvotes: 0
Reputation: 76908
Java is pass by value.
This means that variables hold reference values (just like pointers in C) and when you pass them to a method, that value is copied to a local variable inside the method. Changing what that local variable contains (e.g. setting it to null
) does not affect anything outside that method.
In your example, your method signature is:
deleteMiddleNode(SinglyNode node)
When you call deleteMiddleNode(s5)
from main, the reference value contained in s5
is copied to node
in your method.
Upvotes: 1