I believe its just a copy paste error that you forgot to add the node x to your linked list. Assuming your correct code is like this:
Integer x = new Integer(10);
ll.add(x);
ll.add(4);
You can remove a node from java LinkedList using remove(Object o)
method. So call that to remove node x
from linkedlist ll
.
ll.remove(x); //removes x from linkedlist but does not delete object x
It will remove x
from the linkedlist but object x
is still alive in your code and usable anywhere else.