user2855892
user2855892

Reputation: 81

Not able to set last location of linklist as NULL

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.

Upvotes: 1

Views: 83

Answers (3)

sp4sp7
sp4sp7

Reputation: 58

Assume a linked list, 3->4->6->7->1 and you have to delete the number in last position(5th position),

‘root' is of type ‘Node’ NOT LinkedList. In your code, when you reach last position, variable ‘root’ will hold address of the location where data ‘1’ is stored, say,

root = @addrlocation

when you assign,

root = null

'root' variable points to nothing and in effect you will delete nothing. The key point here is you have to set the 'next' node of last but one node to 'null'

In the above linked list, last but one node which holds data ‘7’, will still holds the address of ’next’ node as @addrlocation. Therefore, the linked list will have no effect from your changes for the particular case you mentioned.

You could have a reference to previous node, ‘previous’ to solve the problem. I have modified your code and pasted below,

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

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

Upvotes: 1

Ben Wainwright
Ben Wainwright

Reputation: 4651

Your problem I think is that you are confused by the semantics of pass by reference. Lets look at the declaration of a Node.

Node foo = new Node();

This statement allocates a new Node object, and assigns the memory address of that object to the variable foo. Note that the variable itself does not contain the object, but a reference to it.

When you pass an object into a method, you are NOT passing the object itself. Instead, a new reference variable is created for that method and the memory address (in this case, saved in foo) is copied into that variable.

So to relate this to your method

static Node deletesNodebyposition(Node root, int position)
{

When this method is called root is created, which is a brand new reference variable. The value of foo is copied into root. Since they BOTH point to the same block of memory, you are able to do things like

root.data = root.next.data;
root.next = root.next.next;

because you are using the reference to go to the previously allocated block of memory. However, the statement

root = null;

Is not setting foo to null, it is setting root to null. Therefore foo is unaffected.

Upvotes: 0

laune
laune

Reputation: 31300

The statement

root = null;

does not have any effect on the linked list. It merely stores null in the parameter location Node root, not in a Node instance variable.

To delete a node in a linked list of Node objects, only statements such as root.next = null will remove a node from the list.

Discussing the handling of linked lists in sufficient detail is a little too much effort for me. Get a good book on data structures, or read Java's implementation of a linked list.

Upvotes: 0

Related Questions