← Skill tree CS Skill Tree 0 CSCD211

Three Node Constructors and Constructor Delegation

Textbook: Liang

Try This First

You want to add the string "Alice" to the front of an existing singly linked list whose head reference is oldHead. Which single line of code does it cleanly?

Reveal
head = new Node<>("Alice", oldHead);

The two-argument constructor sets data = "Alice" and next = oldHead in one expression, so no extra variable is needed.

What You Need To Walk In With

How It Works

The S20 Lab 5 spec calls for exactly three constructors on Node<T>:

public class Node<T> {
    T data;
    Node<T> next;

    // No-arg: both fields null
    public Node() {
        this.data = null;
        this.next = null;
    }

    // One-arg: data set, next defaults to null via delegation
    public Node(final T data) {
        this(data, null);        // delegates to the two-arg form
    }

    // Two-arg: the canonical constructor
    public Node(final T data, final Node<T> next) {
        this.data = data;
        this.next = next;
    }
}

The key relationship is the this(data, null) call in the one-argument constructor. Java requires that a this(...) delegation appear as the very first statement in the constructor body. The compiler resolves it to the two-argument constructor at compile time, so there is no runtime overhead from the indirection.

Pointer picture after Node<String> n = new Node<>("B", null):

  n
  |
  v
+--------+------+
| "B"    | null |
+--------+------+
 data     next

After Node<String> front = new Node<>("A", n):

  front           n
  |               |
  v               v
+-------+----+   +-------+------+
| "A"   |  --+-->| "B"   | null |
+-------+----+   +-------+------+

The two-argument constructor wired the next field to point at the existing node in one step.

Worked Example: Predict, Then Check

After these three lines execute, what are n.data and n.next?

Node<String> n = new Node<>("A");
Reveal

n.data is "A" and n.next is null.

The one-argument constructor delegates to this("A", null), so both fields are set: data receives "A" and next receives null.

A Common Mistake

A tempting pattern when adding to the front of a list is to create a node first and then set its fields separately:

// Avoid this pattern
Node<String> temp = new Node<>();
temp.data = "X";
temp.next = head;
head = temp;

This works, but it bypasses the constructors entirely and scatters the initialization across three statements. If you later change the field names or types, every such call site breaks independently. The fix is to use the two-argument constructor:

head = new Node<>("X", head);

One line, no temporaries, and the node is fully initialized by the time the assignment to head completes. (Source: S20.Lab5-LL_NDH.LinkedList.java)

Go Deeper (optional)

Each new Node<>(...) call allocates one object on the heap: constant time and constant space. When you add to the front of the list using the two-argument form, the total cost is O(1) because no existing nodes are visited. If you are curious why some textbook designs add a dummy head node at the front, that is a separate topic covered later; this course teaches the no-dummy-head form first, where the head reference is either null (empty list) or points directly to the first real node.

Check Yourself

Close the notes and answer each one from memory, then reveal it. Pulling an idea back from memory is one of the strongest ways to make it stick.

Check your understanding

What does this(data, null) inside the one-argument Node constructor do?

Tier 1 · Liang 10e Ch 24

Which constructor form does S20 Lab 5 identify as the canonical one that the others delegate to?

Tier 1 · Liang 10e Ch 24

After Node<Integer> a = new Node<>(5); Node<Integer> b = new Node<>(7, a);, what is b.next.data?

Tier 2 · Liang 10e Ch 24

Why is head = new Node<>(item, head); preferred over Node<T> tmp = new Node<>(); tmp.data = item; tmp.next = head; head = tmp;?

Tier 2 · Liang 10e Ch 24

Where must this(...) appear inside a constructor body?

Tier 3 · Liang 10e Ch 24