I want to delete an entire list which I have created in java(note: I am not using internal list in java.util). I have assigned head to null but my question is will the java garbage collector handle the list which has no head or should I delete every node(I mean setting every node to null) which will be handled by java garbage collector? Following is my code, please let me know which one is ok although both works but I would still like to know. Thanks in advance.

```
//first code
public void deleteList()
{
    Node n = head;
    Node n1;
    head = null;
    System.out.println("Deleting list");
    while(n != null)
    {
        n1 = n;
        n = n.next;
        n1 = null;
    }
    n1 = n;
    n1 = null;
    System.out.println("List deleted");
}

```

```
//second code
public void deleteList()
{
    head = null;
}

```