Задача состоит в том, чтобы добавить элемент в конце связанногоList с помощью boolean
/**
* Appends the specified element to the end of this list
* @param element element to add to the list
* @return true
* @throws NullPointerException when element is null
*/
Мое решение
public boolean add(T element) {
ListNode newElement = new ListNode (element);
while (cur.link == !null) {
cur = cur.link;
cur.link = newElement(element, null);
if (cur==null) {
throw new NullPointerException;
}
}
return true;
}
Нужна помощь в исправлении кода.
Вам нужно будет убедиться, что переменная cur
инициализирована чем-то и изменит следующую строку !=
вместо == !
while (cur.link != null) {
Это поможет с компиляцией @rpax сделать тяжелый подъем.
Вам нужно перейти в конец списка.
public boolean add(T element)
{
ListNode cur= this.firstNode; //or something similar
ListNode newElement = new ListNode (element);
while (cur.link !=null) {
cur=cur.link;
// cur.link = newElement(element, null);
}
//cur.link is null now
cur.link = newElement(element, null);
return true;
}
!=