// CSE 143, Winter 2009, Marty Stepp // ListNode is a class for storing a single node of a linked list. This // node class is for a list of integer values. // Today's version of the class has a recursive toString method. public class ListNode { public int data; // data stored in this node public ListNode next; // link to next node in the list // post: constructs a node with data 0 and null link public ListNode() { this(0, null); } // post: constructs a node with given data and null link public ListNode(int data) { this(data, null); } // post: constructs a node with given data and given link public ListNode(int data, ListNode next) { this.data = data; this.next = next; } // Recursively returns a string representation of this node and // any other nodes after it, such as "30 -> 20 -> 40". public String toString() { String result = "" + this.data; if (next != null) { // there are next node(s) after me result += " -> " + next.toString(); } return result; } }