ListNode 是链表中的一个节点,有一些代码可以更好地理解 ListNode。
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();
list.setNext(soFar);
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();
}
System.out.println();
}
}