0

To remove the last node of the list, I just traversed till the last node and assigned it to null. But I still see the last node value. I am wondering why node = null does not have any effect.

void deleteLastNode(Node node){
    while(node.next != null)
    {
        node = node.next;
    }
    node = null;
}
khelwood
  • 55,782
  • 14
  • 81
  • 108
prabhakar Reddy G
  • 1,039
  • 3
  • 12
  • 23
  • 3
    Because Java arguments are passed by value. The variable `node` that you are reassigning only exists inside your method. – khelwood Nov 07 '19 at 09:32
  • related: [Is it a good practice to change arguments in Java](https://stackoverflow.com/questions/12019978/is-it-a-good-practice-to-change-arguments-in-java) – jhamon Nov 07 '19 at 09:35
  • 1
    However `node.next = null;` *would* have an effect, for reasons explained in the duplicate. – Kayaman Nov 07 '19 at 09:38

0 Answers0