/* ListNode.java */ package list; /** ListNodes are used in building List structure. */ class ListNode { Object item; ListNode next; /** Construct a new ListNode with item it and a null next. */ ListNode(Object it) { item = it; next = null; } /** Construct a new ListNode with item it and a next n. */ ListNode(Object it, ListNode n) { item = it; next = n; } /** Find the node at the given position. * Assumes this list is acyclic. * @return a reference to the node at the given position. * If position < 1 or position > the * size number of nodes in the list, returns null. */ public ListNode ptrTo (int position) { if (position < 1) { return null; } else if (position == 1) { return this; } else if (next == null) { return null; } else { return next.ptrTo(position-1); } } }