Skip to main content
Logo image

Problem Solving with Algorithms and Data Structures using Java: The Interactive Edition

Section 3.21 Implementing an Unordered List: Linked Lists

In order to implement an unordered list, we will construct what is commonly known as a linked list. Recall that we need to be sure that we can maintain the relative positioning of the items. However, there is no requirement that we maintain that positioning in contiguous memory. For example, consider the collection of items shown in Figure 3.21.1. It appears that these values have been placed randomly. If we can maintain some explicit information in each item, namely the location of the next item (see Figure 3.21.2), then the relative position of each item can be expressed by following the link from one item to the next.
Figure 3.21.1. Items Not Constrained in Their Physical Placement
Figure 3.21.2. Relative Positions Maintained by Explicit Links
It is important to note that the location of the first item of the list must be explicitly specified. Once we know where the first item is, the first item can tell us where the second is, and so on. The external reference is often referred to as the head of the list. Similarly, the last item needs to know that there is no next item.

Subsection 3.21.1 The Node Class

The basic building block for the linked list implementation is the node. Each node object must hold at least two pieces of information. First, the node must contain the list item itself. We will call this the data field of the node. In addition, each node must hold a reference to the next node. Listing 3.21.3 shows the Java implementation.
class Node<T> {

    private T data;
    private Node<T> next;

    public Node(T data) {
        this.data = data;
        this.next = null;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Node<T> getNext() {
        return this.next;
    }

    public void setNext(Node<T> next) {
        this.next = next;
    }

    public String toString() {
        return this.data.toString();
    }
}
Listing 3.21.3. The Node Class
To construct a node, you need to supply the initial data value for the node. Evaluating the assignment statement below will yield a Node object containing the value 93 (see Figure 3.21.1). You should note that we will typically represent a node object as shown in Figure 3.21.2.
Node<Integer> temp = new Node(93);
System.out.println(temp.getData());

Note 3.21.4. Java Note.

We have made the data and next properties private. This means the only way for other classes to access and modify these properties is via the getData, setData, getNext and setNext methods. This is a common pattern in Java; we “hide” the properties so that we can change the underlying representation of a class; others who use the class can continue to use the same methods without having to rewrite their code.
The special Java reference value null will play an important role in the Node class and later in the linked list itself. A reference to null will denote the fact that there is no next node. Note in the constructor that a node is initially created with next set to null. Since this is sometimes referred to as “grounding the node,” we will use the standard “electrical ground” symbol to denote a reference that is referring to null. It is always a good idea to explicitly assign null to your initial next reference values.
Figure 3.21.5. A Node Object Contains the Item and a Reference to the Next Node
Figure 3.21.6. A Typical Representation for a Node

Subsection 3.21.2 The UnorderedList Class

As we suggested above, the unordered list will be built from a collection of nodes, each linked to the next by explicit references. As long as we know where to find the first node (containing the first item), each item after that can be found by successively following the next links. With this in mind, the UnorderedList class must maintain a reference to the first node. Listing 3.21.7 shows the constructor. Note that each list object will maintain a single reference to the head of the list.
class UnorderedList<T> {

    private Node<T> head;

    public UnorderedList() {
        head = null;
    }
Listing 3.21.7. The UnorderedList Class
Initially when we construct a list, there are no items. The assignment statement
UnorderedList<Integer> myList = new UnorderedList<>()
creates the linked list representation shown in Figure 3.21.8. As we discussed in the Node class, the special reference null will again be used to state that the head of the list does not refer to anything. Eventually, the example list given earlier will be represented by a linked list as shown in Figure 3.21.9. The head of the list refers to the first node which contains the first item of the list. In turn, that node holds a reference to the next node (the next item), and so on. It is very important to note that the list class itself does not contain any node objects. Instead it contains a single reference to only the first node in the linked structure.
Figure 3.21.8. An Empty List
Figure 3.21.9. A Linked List of Integers
The isEmpty method, shown in Listing 3.21.10, checks to see if the head of the list is a reference to null. The result of the boolean expression this.head == null will only be true if there are no nodes in the linked list. Since a new list is empty, the constructor and the check for empty must be consistent with one another. This shows the advantage to using the reference null to denote the end of the linked structure. In Java, null can be compared to any reference. Two references are equal if they both refer to the same object. We will use this often in our remaining methods.
public boolean isEmpty() {
    return this.head == null;
}
Listing 3.21.10.
So how do we get items into our list? We need to implement the add method. However, before we can do that, we need to address the important question of where in the linked list to place the new item. Since this list is unordered, the specific location of the new item with respect to the other items already in the list is not important. The new item can go anywhere. With that in mind, it makes sense to place the new item in the easiest location possible.
Recall that the linked list structure provides us with only one entry point, the head of the list. All of the other nodes can only be reached by accessing the first node and then following next links. This means that the easiest place to add the new node is right at the head, or beginning, of the list. In other words, we will make the new item the first item of the list, and the existing items will need to be linked to this new first item so that they follow.
The linked list shown in Figure 3.21.9 was built by calling the add method a number of times.
myList.add(31);
myList.add(77);
myList.add(17);
myList.add(93);
myList.add(26);
myList.add(54);
Note that since 31 is the first item added to the list, it will eventually be the last node on the linked list as every other item is added ahead of it. Also, since 54 is the last item added, it will become the data value in the first node of the linked list.
The add method is shown in Listing 3.21.11. Each item of the list must reside in a node object. Line 2 creates a new node and places the item as its data. Now we must complete the process by linking the new node into the existing structure. This requires two steps as shown in Figure 3.21.12. Step 1 (line 3) changes the next reference of the new node to refer to the old first node of the list. Now that the rest of the list has been properly attached to the new node, we can modify the head of the list to refer to the new node. The assignment statement in line 4 sets the head of the list.
public void add(T item) {
    Node<T> temp = new Node<T>(item);
    temp.setNext(this.head);
    this.head = temp;
}
Listing 3.21.11. Adding a Node to a Linked List
Figure 3.21.12. Adding a New Node is a Two-Step Process
The order of the two steps described above is very important. What happens if the order of line 3 and line 4 is reversed? If the modification of the head of the list happens first, the result can be seen in Figure 3.21.13. Since the head was the only external reference to the list nodes, all of the original nodes are lost and can no longer be accessed.
Figure 3.21.13. Result of Reversing the Order of the Two Steps
The next methods that we will implement–size, search, and remove–are all based on a technique known as linked list traversal. Traversal refers to the process of systematically visiting each node. To do this we use an external reference that starts at the first node in the list. As we visit each node, we move the reference to the next node by “traversing” the next reference.
To implement the size method, we need to traverse the linked list and keep a count of the number of nodes that occurred. Listing 3.21.14 shows the Java code for counting the number of nodes in the list. The external reference is called current and is initialized to the head of the list in line 2. At the start of the process we have not seen any nodes, so the count is set to zero. Lines 4–7 actually implement the traversal. As long as the current reference has not seen the end of the list (null), we move current along to the next node via the assignment statement in line 6. Again, the ability to compare a reference to null is very useful. Every time current moves to a new node, we add 1 to count. Finally, count gets returned after the iteration stops. Figure 3.21.15 shows this process as it proceeds down the list.
public int size() {
    Node<T> current = this.head;
    int count = 0;
    while (current != null) {
        count = count + 1;
        current = current.getNext();
    }
    return count;
}
Listing 3.21.14. List Size
Figure 3.21.15. Traversing the Linked List from the Head to the End
Searching for a value in a linked list implementation of an unordered list also uses the traversal technique. As we visit each node in the linked list, we will ask whether the data stored there matches the item we are looking for. In this case, however, we may not have to traverse all the way to the end of the list. In fact, if we do get to the end of the list, that means that the item we are looking for must not be present. Also, if we do find the item, there is no need to continue.
Listing 3.21.16 shows the implementation for the search method. As in the size method, the traversal is initialized to start at the head of the list (line 2). We continue to iterate over the list as long as there are more nodes to visit. The question in line 4 asks whether the data item is present in the current node. If so, we return true immediately.
As an example, consider invoking the search method looking for the item 17.
boolean found = myList.search(17);
System.out.println(found); // prints true
Since 17 is in the list, the traversal process needs to move only to the node containing 17. At that point, the condition in line 4 becomes true and we return it as the result of the search. This process can be seen in Figure 3.21.17.
Figure 3.21.17. Successful Search for the Value 17
The remove method requires two logical steps. First, we need to traverse the list looking for the item we want to remove. Once we find the item, we must remove it. If the item is not in the list, our method should throw a NoSuchElementException.
The first step is very similar to search. Starting with an external reference set to the head of the list, we traverse the links until we discover the item we are looking for.
When the item is found and we break out of the loop, current will be a reference to the node containing the item to be removed. But how do we remove it? One possibility would be to replace the value of the item with some marker that suggests that the item is no longer present. The problem with this approach is the number of nodes will no longer match the number of items. It would be much better to remove the item by removing the entire node.
In order to remove the node containing the item, we need to modify the link in the previous node so that it refers to the node that comes after current. Unfortunately, there is no way to go backward in the linked list. Since current refers to the node ahead of the node where we would like to make the change, it is too late to make the necessary modification.
The solution to this dilemma is to use two external references as we traverse down the linked list. current will behave just as it did before, marking the current location of the traversal. The new reference, which we will call previous, will always travel one node behind current. That way, when current stops at the node to be removed, previous will refer to the proper place in the linked list for the modification.
Listing 3.21.18 shows the complete remove method. Lines 2–3 assign initial values to the two references. Note that current starts out at the list head as in the other traversal examples. previous, however, is assumed to always travel one node behind current. For this reason, previous starts out with a value of null since there is no node before the head (see Figure 3.21.19).
In line 5 we decide whether to continue the loop or not. We continue if it we haven’t reached the end of the list (the first condition) and the item at the current location isn’t the one we’re looking for (the second condition). If we do not find the item, previous and current must both be moved one node ahead.
Again, the order of these two statements is crucial. previous must first be moved one node ahead to the location of current. At that point, current can be moved. This process is often referred to as inchworming, as previous must catch up to current before current moves ahead. Figure 3.21.20 shows the movement of previous and current as they progress down the list looking for the node containing the value 17.
public void remove(T item) {
    Node<T> current = this.head;
    Node<T> previous = null;

    while (current != null && (!current.getData().equals(item))) {
        previous = current;
        current = current.getNext();
    }
    if (current != null) {
        if (previous == null) {
            this.head = current.getNext();
        } else {
            previous.setNext(current.getNext());
        }
    }
}
Listing 3.21.18. Removing an Item from a Linked List
Figure 3.21.19. Initial Values for the previous and current References
Figure 3.21.20. previous and current Move Down the List
Once the searching step of the remove has been completed, we need to remove the node from the linked list. Figure 3.21.21 shows the link that must be modified. However, there is a special case that needs to be addressed. If the item to be removed happens to be the first item in the list, then current will reference the first node in the linked list. This also means that previous will be null. We said earlier that previous would be referring to the node whose next reference needs to be modified in order to complete the removal. In this case, it is not previous but rather the head of the list that needs to be changed (see Figure 3.21.22). Another special case occurs if the item is not in the list. In that case current != null evaluates to false and we leave the list unchanged.
Figure 3.21.21. Removing an Item from the Middle of the List
Figure 3.21.22. Removing the First Node from the List
Line 14 allows us to check whether we are dealing with the special case described above. If previous did not move, it will still have the value null when the loop breaks. In that case, the head of the list is modified to refer to the node after the current node (line 15), in effect removing the first node from the linked list.
However, if previous is not null, the node to be removed is somewhere down the linked list structure. In this case the previous reference is providing us with the node whose next reference must be changed. Line 17 modifies the next property of the previous to accomplish the removal. Note that in both cases the destination of the reference change is current.next. One question that often arises is whether the two cases shown here will also handle the situation where the item to be removed is in the last node of the linked list. We leave that for you to consider.

Note 3.21.23. Java Note.

Line 5 of remove deserves a closer look:
while (current != null && (!current.getData().equals(item))) {
We want to keep looping as long as we haven’t hit the end of the list and haven’t found the item we want to delete. But what if current is null? Why doesn’t the getData() call fail? The answer is short-circuit evaluation. When Java evaluates boolean expressions, it works from left to right. As soon as a final result can be determined, Java stops.
When current != null is true, Java has to evaluate the second condition to determine the result. But when current is null, the first condition returns false. That means that, no matter what the second condition is, the result has to be false (it can only be true when both conditions are true). Java doesn’t need to look at the second condition, and immediately yields false for the expression. The getData call never happens.
In line 10, we wrote the fully qualified name java.util.NoSuchElementException. When a name is fully qualified, you don’t need the import statement at the start of the file.
Listing 3.21.24 is the complete UnorderedList class, with comments to document the methods:
class UnorderedList<T> {

    private Node<T> head;

    /* Construct an empty list */
    public UnorderedList() {
        head = null;
    }

    /* Returns the head of the list */
    public Node<T> getHead() {
        return this.head;
    }

    /* Sets the head of the list to the given Node */
    public void setHead(Node<T> newHead) {
        this.head = newHead;
    }

    /* Return true if list is empty, false otherwise */
    public boolean isEmpty() {
        return this.head == null;
    }

    /*
     * Add given item at the head of the list.
     * Presume that the item is not already in the list.
     */
    public void add(T item) {
        Node<T> temp = new Node<T>(item);
        temp.setNext(this.head);
        this.head = temp;
    }

    /* Return the number of items in the list */
    public int size() {
        Node<T> current = this.head;
        int count = 0;
        while (current != null) {
            count = count + 1;
            current = current.getNext();
        }
        return count;
    }

    /*
     *  Check to see if the given item is in the list.
     * Return true if it is, false if not.
     */
    public boolean search(T item) {
        Node<T> current = this.head;
        while (current != null) {
            if (current.getData().equals(item)) {
                return true;
            }
            current = current.getNext();
        }
        return false;
    }

    /*
     * Remove the given item from the list.
     * Throws a NoSuchElementException if the item is not in the list
     */
    public void remove(T item) {
        Node<T> current = this.head;
        Node<T> previous = null;

        while (current != null && (!current.getData().equals(item))) {
            previous = current;
            current = current.getNext();
        }

        if (current != null) {
            if (previous == null) {
                this.head = current.getNext();
            } else {
                previous.setNext(current.getNext());
            }
        }
    }

    /*
     * Convert the list to a comma-separated series of
     * values in brackets, starting with the head of the list.
     */
    public String toString() {
        String result = "[";
        Node<T> current = this.head;
        while (current != null) {
            result = result + current.getData().toString();
            current = current.getNext();
            if (current != null) {
                result = result + ", ";
            }
        }
        result = result + "]";
        return result;
    }
}
Listing 3.21.24. The Node and UnorderedList Classes
Listing 3.21.25 shows a program to test the UnorderedList class.
Listing 3.21.25. Test of UnorderedList Methods
The remaining methods append, insert, index, and pop are left as exercises. Remember that each of these must take into account whether the change is taking place at the head of the list or someplace else. Also, insert, index, and pop require that we name the positions of the list. We will assume that position names are integers starting with 0.

Exercises Self Check

Part I: Implement the append method for UnorderedList. What is the time complexity of the method you created?
Part II: In the previous problem, you most likely created an append method that was \(O(n)\) If you add an instance variable to the UnorderedList class you can create an append method that is \(O(1)\text{.}\) Modify your append method to be \(O(1)\) Be Careful! To really do this correctly, you will need to consider a couple of special cases that may require you to make a modification to the add method as well.
You have attempted of activities on this page.