package DataStructure;
//用数组创建链表
public class ListNode<E> {
private E val;
private ListNode next;
public ListNode(E val){
this.val = val;
}
//链表节点的构造函数
public ListNode(E[] arr){
if(arr == null || arr.length == 0){
throw new IllegalArgumentException("arr is empty");
}
this.val = arr[0];
ListNode cur = this;
for (int i = 1; i < arr.length; i++) {
cur.next = new ListNode(arr[i]);
cur = cur.next;
}
}
@Override
public String toString(){
StringBuilder res = new StringBuilder();
ListNode cur = this;
while(cur != null){
res.append(cur.val+"->");
cur = cur.next;
}
res.append("null");
return res.toString();
}
}
java用数组实现链表
最新推荐文章于 2025-02-09 22:51:55 发布

494

被折叠的 条评论
为什么被折叠?



