为什么每次我们创建新节点p.next所以我们需要为this.next分配null?并不总是空的吗?它在LinkedList中扮演的角色是什么?
如果我们尝试打印出this.next,在将它指定为null之前它将为null.
System.out.println(this.next);
结果为空
另外,如果p.next指向新节点,为什么我们需要设置p = p.next指向同一个节点?如果最后将tail设置为p的目的,我们不能只设置tail = p.next,这是for循环结束后的最后一个.
public class EnkeltLenketListe{
private T value;
private Node next;
private Node(T value, Node next)
{
System.out.println(this.next);
this.next = next;
this.value = value;
}
}
private Node head, tail;
public EnkeltLenketListe(T[] a)
{
this();
int i = 0; for (; i < a.length && a[i] == null; i++);
if (i < a.length)
{
head = new Node<>(a[i], null);
Node p = head;
for (i++; i < a.length; i++)
{
if (a[i] != null)
{
p.next = new Node<>(a[i], null);
p = p.next;
}
}
tail = p;
}
}