Advertisement
Aldin-SXR

removeFromRear()

Mar 5th, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. /* Remove an item from the end of the list */
  2. public void removeFromRear() {
  3.     if (head == null) {                                                     // 1
  4.         throw new IndexOutOfBoundsException("The linked list is empty.");   // 1
  5.     } else if (size == 1) {                                                 // 2
  6.         head = null;                                                        // 2
  7.     } else {                                                                // 3
  8.         Node<Item> current = head;                                          // 3
  9.         while (current.next.next != null) {                                 // 4
  10.             current = current.next;                                         // 4
  11.         }                                                                   // 4
  12.         current.next = null;                                                // 5
  13.     }
  14.     size--;                                                                 // 6
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement