力扣中常用的只有属性和构造函数
实际测试非常需要add()来初始化ListNode,需要print()检查结果
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
// 添加结点
public void add(int newdata) {
ListNode newNode = new ListNode(newdata);
if (this.next == null) {
this.next = newNode;
} else {
this.next.add(newdata);
}
}
public void print() {
System.out.println(this.val + "-->");
if (this.next != null) {
this.next.print();
}
}
}



