您好,我正在实现一个单链表,而不使用库。我遇到了在列表末尾添加元素的问题。谁能帮助我。这是我的代码:
public class SinglyLinkedList<T> implements GenericLinkedList<T> {
private Node<T> head;
private Node<T> tail;
private int size;
public SinglyLinkedList() {
head = null;
tail = null;
size = 0;
}
public void addLast(T elem) {
tail.next.element = elem;
size++;
}
**public void addLast(T elem) {
Node<T> newNode = new Node<T>();
tail.next = null;
newNode.element = elem;
**