I am trying to code a method for deleting the last Node in a linkedlist(for learning how to modify a linkedlist..I am not using the java library LinkedList class)..I tried to deal with the usecase where the passed linkedlist has only one Node.
However when I try to print the linkedlist before and after the removal, it gives the same output as if the node was not removed.
class NodeProcessing{
public static void removeLastNode(Node f){
if (f==null) return;
if(f.next == null){//if linkedlist has single node
f = null;
return;
}
...
}
public static void showList(Node first){
System.out.println("linked list=");
for(Node x = first; x != null; x = x.next){
System.out.print(x.item+" ,");
}
System.out.println();
}
public static void main(String[] args) {
Node a = new Node();
a.item = "one";
showList(a);
removeLastNode(a);
showList(a);
}
}
class Node{
String item;
Node next;
}
Output:
linked list= one ,
linked list= one ,
update:
when I used the debugger,I can see that the Node a
in main()
has address :Node@a61164
and the Node f
inside removeLastNode()
also has :Node@a61164