I wrote the code for deleting a node at a certain position from linklist.

```
static Node deletesNodebyposition(Node root,int position)
{
    if(root == null)
        return null;

    Node head = root;
    int x=1;
    while(x < position && root !=null){
        root = root.next;
        x++;
    }
    if(root.next !=null) {
        root.data = root.next.data;
        root.next = root.next.next;
    }
    else
        root = null;

    return head;
}

```

The code works fine until I choose the last Node to delete. When I enter the last position to delete the node, I am trying to set the node as NULL. But when I return from function and print my result list, I still find the last Node. I am not able to understand why the last node is not able to set as NULL.