Skip to content

Latest commit

 

History

History
45 lines (35 loc) · 698 Bytes

Ex_1_3_19.md

File metadata and controls

45 lines (35 loc) · 698 Bytes
title date draft tags categories
Algorithm4 Java Solution 1.3.19
2019-07-04 05:47:10 +0800
false
JAVA
TECH
archives

1.3.19

Problem:

Give a code fragment that removes the last node in a linked list whose first node is first.

Solution:

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;
      }
}

Reference: