APCS Java Subset

Class ListNode

  extended byap.ListNode

public class ListNode
extends java.lang.Object

The class for linked list nodes that will be used and tested on the Advanced Placement Computer Science exam. For example, the two questions below, similar to Questions 15 and 33 on the 1992 released AB exam, show some of the ListNode methods in use. These are shown here as two questions emerging from common code. On the released exam these questions were not related.

(Question 15) Consider the static methods print and create in the class List below. What is printed by the call List.print(List.create(5)) ?

  1. 5 4 3 2 1
  2. 1 2 3 4 5
  3. 5 1
  4. 5
  5. 1

(Question 33) Consider the static method process in the class List below. Suppose p represents the list (1,2,3,4); what is returned by the call List.process(p,null)?

  1. null
  2. (1)
  3. (1,1,1,1)
  4. (4,3,2,1)
  5. (1,2,3,4)

 public class List
     // return a linked list based on parameters list and soFar

     public static ListNode process(ListNode list, ListNode soFar)
         if (list == null) {
             return soFar;
         else {
             ListNode temp = list.getNext();
             return process(temp,list);

      // return a new linked list based on parameter n

      public static ListNode create(int n)
	    ListNode list = null;
	    for(int k=1; k <= n; k++) {
	        list = new ListNode(new String(""+k), list);
  	    return list;

      // print a linked list

      public static void print(ListNode list)
	    while (list != null) {
	        System.out.print(list.getValue()+" ");
	        list = list.getNext();

Constructor Summary
ListNode(java.lang.Object initValue, ListNode initNext)
          Construct a list node with specified value and next node.
Method Summary
 ListNode getNext()
          Returns the next node after this node.
 java.lang.Object getValue()
          Returns the value stored in this node.
 void setNext(ListNode theNewNext)
          Sets the next node after this node
 void setValue(java.lang.Object theNewValue)
          Sets the value stored in this node.
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Detail


public ListNode(java.lang.Object initValue,
                ListNode initNext)
Construct a list node with specified value and next node.

initValue - is the value in the constructed node
initNext - is the value of the next node (returned by getNext())
Method Detail


public java.lang.Object getValue()
Returns the value stored in this node.

this node's value


public ListNode getNext()
Returns the next node after this node.

the node after this node


public void setValue(java.lang.Object theNewValue)
Sets the value stored in this node.

theNewValue - is the (new) value of this node


public void setNext(ListNode theNewNext)
Sets the next node after this node

theNewNext - is the (new) next node after this node

unofficial documentation for the APCS Java Subset