Q:

Learning Links and Linked Data Structures Java

I am learning Linked Data Structures and the concept makes sense to me, however the code does not.
I was hoping someone could explain this for me.
This is from lecture slides:
I have a constructor here:
private static class Node<E> {
    private E value;
    private Node<E> next;
    private Node( E value, Node<E> next ) {
        this.value = value;
        this.next = next;
    }
}

Value- stores actual information. 
Next- stores reference to the previous object stored. Makes sense. 
But then...
public void addFirst( E o ) {
    Node<E> newNode = new Node<E>( o, null );
    if ( head == null ) {
        head = newNode;
    } else {
        newNode.next = head;
        head = newNode;
    }
}

The above code is supposed to be adding elements.
But here is my problem: newNode created. newNode.next field stores the reference to object head. (by the way, I have no idea where head is declared) and then: head=newNode.
So Head now has the value that was stored by newNode, and head.next is now referencing itself?
The problem here is that the original value in head is lost, and there is no reference to newNode.
Am I missing something?

A:

I think you're missing the objects' identities. Consider the following classes with their fields:
List<E>
    Node<E> head
Node<E>
    E value
    Node<E> next

Let's say you have an Empty List, which means there are these objects (actually just one):
List(1)
    head = null

To better illustrate, I gave the object a unique number. In reality, an object is identified unambiguously by its memory location.
Now add "as" to the list:
Node<E> newNode = new Node<E>( o, null );
List(1)
    head = null
Node(2)
    value = "as"
    next = null

head = newNode;
List(1)
    head = Node(2)
Node(2)
    value = "as"
    next = null

and now add "df":
Node<E> newNode = new Node<E>( o, null );
List(1)
    head = Node(2)
Node(2)
    value = "as"
    next = null
Node(3)
    value = "df"
    next = null

newNode.next = head;
List(1)
    head = Node(2)
Node(2)
    value = "as"
    next = null
Node(3)
    value = "df"
    next = Node(2)

head = newNode;
List(1)
    head = Node(3)
Node(2)
    value = "as"
    next = null
Node(3)
    value = "df"
    next = Node(2)

As you can see, the single node's values were not changed, and the new head, Node(3) does not reference itself. head has value "df", the next node has "as", so "df" was indeed added in front of the existing node.

As others have also noted, addFirst could be greatly simplified. Look at the original:
Node<E> newNode = new Node<E>( o, null );
if ( head == null ) {
    head = newNode;
} else {
    newNode.next = head;
    head = newNode;
}

The head = newNode; line appears at the bottom of both of the if's branches, so it can be moved out:
Node<E> newNode = new Node<E>( o, null );
if ( head == null ) {
} else {
    newNode.next = head;
}
head = newNode;

...which leaves the then branch empty, so better reverse the condition:
Node<E> newNode = new Node<E>( o, null );
if ( head != null ) {
    newNode.next = head;
}
head = newNode;

Looking closely, after that if, newNode.next will be the (old) head regardless of whether the if was executed (then it's obvious) or not (the constructor set it to null, and if the if isn't executed, then head was null too). So another way to write this would be:
Node<E> newNode = new Node<E>( o, head );
head = newNode;

And now you have a variable that's only used once:
head = new Node<E>( o, head );

But really, write the code the way you can understand it better, especially if your objective is learning.

