title | date | draft | tags | categories | |||
---|---|---|---|---|---|---|---|
Algorithm4 Java Solution 1.3.19 |
2019-07-04 05:47:10 +0800 |
false |
|
|
Give a code fragment that removes the last node in a linked list whose first node is first.
public Item deleteAtLast() {
if (isEmpty()) {
return null;
} else if (size() == 1) {
Item ret = first.item;
first = null;
N--;
return ret;
} else {
Node x = first;
for (; x.next.next != null; x = x.next) {
}
Item ret = x.next.item;
x.next = null;
N--;
return ret;
}
}