user862226
user862226

Reputation: 129

java LinkedList deletion give me different values

I am sending a LinkedListNode into a method (removeLinkListNodeDuplication) to remove duplicates entry. It iterate through the LinkedListNode and find the duplicate value with the help of Hashtable.

I wonder why the instance of my LinkedListNode is null at the end of my delete method(removeLinkListNodeDuplication), but after it comes out of my deletion method it change to my expectation.

Here is my LinkedListNode class

public class Node {
Node next = null;
int data;

public Node(int d) {
    data = d;
}

public void appendToTail(int d) {
    Node end = new Node(d);
    Node n = this;
    while (n.next != null) {
        n = n.next;

    }
    n.next = end;
}

}

and in below I am creating a instance of above class and pass it to my delete method.

Node linkList =new Node(1);
    linkList.appendToTail(1);
    linkList.appendToTail(1);
    linkList.appendToTail(2);
    removeLinkListNodeDuplication(linkList);
    // link list values are as expected i.e. 1,2 ?!

and below is my delete method

public void removeLinkListNodeDuplication(Node listNode) {
    Node previous =null;
    Hashtable<Integer,Boolean> hashtable=new Hashtable<Integer, Boolean>();
    while (listNode!=null){
        if(hashtable.containsKey(listNode.data)){
            previous.next =listNode.next;
        }
        else{
            hashtable.put(listNode.data,true);
            previous =listNode;
        }

        listNode = listNode.next;
    }
    //listNode is null 

}

Upvotes: 1

Views: 243

Answers (2)

Andromeda
Andromeda

Reputation: 12897

Your while loop is executed till the last Node (listNode!=null)

So For the last iteration, listNode.next will be null and the same is assigned to listNode.

Before calling removeLinkListNodeDuplication(linkList);, linkList refers to the address of first Object.

Inside removeLinkListNodeDuplication(linkList); at the last iteration, listNode refers to the last Node, and you are making that Node as Null. It will not modify the previous Nodes.

So after the execution of removeLinkListNodeDuplication(linkList);, linkList still refers to the firstNode which is not modified. Only thing that was modified during the removeLinkListNodeDuplication exectuion was the link from firstNode to the nextNode which is now pointing to the lastNode after skipping the duplicateNode. Please check the comments inside the code snippet which explains it in detail.

Node linkList =new Node(1); // linkList now Points to 1stNode
linkList.appendToTail(1); // linkList still points to 1stNode - add a link inside 1st Node to 2nd Node
linkList.appendToTail(1); // linkList still points to 1stNode - add a link inside 2st Node to 3rd Node
linkList.appendToTail(2); // linkList still points to 1stNode - add a link inside 3rd Node to 4th Node
DateTest dt = new DateTest();
dt.removeLinkListNodeDuplication(linkList);

//linkList still points to 1stNode - but the link is now modified from 2ndNode to 4thNode




public void removeLinkListNodeDuplication(Node listNode) {
    Node previous =null;
    Hashtable<Integer,Boolean> hashtable=new Hashtable<Integer, Boolean>();
    while (listNode!=null){
        if(hashtable.containsKey(listNode.data)){
            previous.next =listNode.next; 
            // 2nd Iteration - previous points to 1stNode - modify link inside 1stNode to 3rd Node
            // 3rd Iteration - previous points to 1stNode - modify link inside 1stNode to 4th Node

        }
        else{
            hashtable.put(listNode.data,true);
            previous =listNode; 
            //1st Iteration previous points to 1stNode
            //4thIteration  previous points to 4thNode
        }

        listNode = listNode.next; 
        //1st Iteration listNode points to 2ndNode
        //2nd Iteration listNode points to 3rdNode
        //3rd Iteration listNode points to 4thNode
        //4th Iteration listNode points to Null
    }
    //listNode is null ?!
    // listNode now points to Null

}

Upvotes: 1

krishna vijayaram
krishna vijayaram

Reputation: 1

Hi the linklist in method is a reference to the object whose value is assigned by the parameter.

I think you are talking about call by reference.

If you modify the content of the object. You could get that in the calling method.

But if you modify the reference itself you can't get that back in the calling method.

Upvotes: 0

Related Questions