Java Reference
In-Depth Information
data
3
next
data
5
next
data
2
next
/
front
current
At that point, you can assign current.next to be a new node with 17 in it:
current.next = new ListNode(17);
In this case, you are changing the currently null value in that last node to instead
link to a new node with 17 in it:
data
3
next
data
5
next
data
2
next
data
17
next
/
front
current
You have been preparing to write code for the appending add . So this code would
be included inside a method, and you would have to alter it to use the name of the
parameter:
public void add(int value) {
ListNode current = front;
while (current.next != null) {
current = current.next;
}
current.next = new ListNode(value);
}
Even this code isn't quite correct, because you have to deal with the special case in
which the list is empty:
public void add(int value) {
if (front == null) {
front = new ListNode(value);
} else {
ListNode current = front;
while (current.next != null) {
current = current.next;
}
current.next = new ListNode(value);
}
}
Search WWH ::




Custom Search