educative.io

Educative

Using this.headNode vs headNode

Confused why the example code for a Doubly Linked list (Java) at times references the headNode reference variable as “this.headNode” and then at other times uses “headNode”. Also, tailNode is never referenced with “this”. See code snippet below for an example

public void insertAtHead(T data) {
        Node newNode = new Node();
        newNode.data = data;
        newNode.nextNode = this.headNode; //Linking newNode to head's nextNode
        newNode.prevNode = null; //it will be inserted at start so prevNode will be null
        if (!isEmpty())
            headNode.prevNode = newNode;
        else
            tailNode = newNode;
        this.headNode = newNode;
        size++;
    }

There is no major difference between this.headNode and headNode. this.headNode simply reinforces that it is referring to the same class node.