← Linked lists CS Skill Tree

Reaching the end of a dummy-head list

The lessons behind it are A run of items, added or removed and Adding a node, every case.

The method moveFirstToEnd takes the node at the front of a list and attaches that same node at the end. The list has a node in front of the first value that holds no value of its own (a dummy head).

Your readiness not started

Your own marking on this problem.

The contract

public void moveFirstToEnd()

What the class already has:

List beforeList aftersize after
emptyempty0
redred1
red greengreen red2
red green bluegreen blue red3

The node that held red at the front is the same node object that sits at the end afterward. Nothing is copied, and size never changes.

The idea

  1. Put first on head.next. If first is null, the list is empty, so return.
  2. Point head.next at first.next. Now no node in the list points at first (it is unlinked).
  3. Set first.next to null. first is about to be the last node, and the last node points at null.
  4. Put last on head, the dummy. Move last forward while last.next is not null.
  5. Point last.next at first.
  6. Leave size alone. The list holds the same nodes as before.

Step 4 starts on the dummy so that a 1-node list needs no if. After step 2, a list that had 1 node is empty, because head.next is now null. The walk stops at once with last on the dummy, and step 5 sets head.next back to first. A walk that started on head.next would start on null in that case and throw at last.next.

public void moveFirstToEnd() {
    Node first = head.next;
    if (first == null) {
        return;
    }
    head.next = first.next;
    first.next = null;
    Node last = head;
    while (last.next != null) {
        last = last.next;
    }
    last.next = first;
}

A trace of the method on red green blue, with the list read from head.next:

Linelast is onWhat changedListsize
Node first = head.next;not setfirst is on redred green blue3
head.next = first.next;not setthe dummy points at green; red still points at greengreen blue3
first.next = null;not setred points at nullgreen blue3
Node last = head;dummythe walk startsgreen blue3
loop, turn 1greenlast movedgreen blue3
loop, turn 2bluelast moved; blue.next is null, so the loop endsgreen blue3
last.next = first;blueblue points at redgreen blue red3

The cases

CaseList beforePrinted afterWhat it checks
EmptyemptyList size is: 0the guard returns before first.next is read
1 noderedList size is: 1
red
the walk from the dummy puts red back
2 nodesred greenList size is: 2
green
red
the loop runs 1 turn
3 nodesred green blueList size is: 3
green
blue
red
the usual case
Equal values at the frontred red greenList size is: 3
red
green
red
the method moves a node, so equal values do not matter
2 calls on 3 nodesred green blueList size is: 3
blue
red
green
the end of the list is still marked by null after the first call
3 calls on 3 nodesred green blueList size is: 3
red
green
blue
the list comes back to where it started

Drill

Work each one on paper before you tap.

Level-up check

1. Predict the output. The list holds ant bee cat dog front to back. moveFirstToEnd() runs once and the list is printed. What comes out?

2. Spot the bug. This version starts the walk on head.next instead of the dummy. It runs on the 1-node list ant. What happens?

Node first = head.next;
if (first == null) {
    return;
}
head.next = first.next;
first.next = null;
Node last = head.next;
while (last.next != null) {
    last = last.next;
}
last.next = first;

3. Pointer order. The 2 unlink lines are written in the other order: first.next = null; first, then head.next = first.next;. Everything else is the same. The list holds ant bee cat. What is printed?

4. An edge case. Someone deletes the if (first == null) block and calls the method on an empty list. What happens?

4 questions. 3 right is a pass.

Write itThe walk to the last node

Write the lines that leave last on the last node of a dummy-head list. They must also work when head.next is null.

Write itLeaving out first.next = null

Someone leaves out the line first.next = null;. The list holds ant bee cat. Write which node points at which when the method ends, and say what toString does.

Your checklist

Tick these when they are true.

Could you write this cold?

Blank file, no notes, 4 hours on a clock, and only the Java API documentation open. Say yes only when that is true. You can take it back at any time.

Rules on the practice APE

You change only the lines between //startMethod and //endMethod, and you keep each method header as given.

A private helper method or field of your own between the markers is fine.

No new imports.

The official site's Basic Strategies page says: You may receive points for compiling code but you WON’T receive points for code that does not compile.

The addLast problem in Practice exam 2 and the addAll problem in Practice exam 1 use these moves.

Further reading