### Deleting a node at the tail of a list

• The general case to delete a Node at the tail of a list (= list has a lot of nodes)

• Before the deletion takes place:

• We have a non-empty list:

• After the deletion has taken place:

• We must have a list where the last Node object is unchained from the list --- like this:

Note:

• The links must be unchained (disconnected) in the one-before-last node in the linked list !!

 You do not know which node is the one-before-the-last node in the linked list !!!

• Therefore, we must first find the one-before-the-last node in the list in order to make the necessary links !!!

• Finding the "one-before-the-last" node

• Fact:

 You can only more forward down the list You cannot go backward in a (simple) linked list

• We can find the last node in a list as follows:

 ``` ptr = head; while ( ptr.next != null ) // Last node has next == null { ptr = ptr.next; } ```

Result:

However:

 It is impossible to access the one-before-the-last node from the last node !!!

• Programming trick to find the one-before-the-last node:

• Use 2 reference variables ptr1 and ptr2 that points to consecutive Nodes:

How to use these pointers:

 ptr1 will always point to the Node before the node pointed to by ptr2 We use ptr2 to find the last node. When ptr2 points to the last node, the variable ptr1 will point to the "one-before-the-last" node

• Algorithm to find the one-before-the-last node:

 ``` 1. Initialization: Make ptr1 points to the first Node Make ptr2 points to the second Node 2. While ( ptr2 ≠ last node ) do: { 1. move ptr1 forward 2. move ptr2 forward } When the while loop ends, we will have: ptr2 points to the last node ptr1 points to the one-before-the-last node ```

• Statements to delete the last node from a list: the general case

• Once that we have found the one-before-the-last node, deleting the last node is done as follows (as discussed above):

• Before the deletion:

• After the deletion:

Statement(s) to achieve this result:

 ``` ptr1.next = null; ```

• Algorithm to delete the last node for the general case:

 ``` // Find the last *2* Nodes in the list /* ------------------------------------------------ Initial state: head ---> Node1 ---> Node2 ---> Node3 --->... ^ ^ | | ptr1 ptr ------------------------------------------------- */ Node ptr, ptr1; // ptr1 always points to the Node // before the one pointed to by ptr ptr1 = head; // ptr1 points to first node ptr = head.next; // ptr points to node following ptr1 while ( ptr.next != null ) { ptr1 = ptr; // Move to next pair of node ptr = ptr.next; } // ****************************************************** // ptr NOW points to the last Node in list // ptr1 NOW points to the Node BEFORE the last Node // ****************************************************** r = ptr.value; // Save the the value in last Node // (in case you want to return it) ptr1.next = null; // Mark the one-before-last node as the new last node ```

• The special case to delete a Node at the tail of a list (when we need to update head)

• The only time that we must update the head variable when we delete a node at the tail is:

 The list has exactly one node !!!

• A list with exactly one node is detected as follows:

 ``` if ( head.next == null ) Reason: head.next is the link in the first node If this link is null, then the first node is also the last node. The list has exactly 1 node.... ```

• After deleting the only node from a list, the list will become empty

An empty list is indicated using:

 ``` head = null; ```

• The remove() method to delete at the tail of a list

• The method that deletes the node at the tail of a list:

 ``` public class List { Node head; // Head is used to access the nodes // in the linked list /* ================================================ Constructor: initialize head to represent an empty list ================================================= */ public List() { head = null; // null means: no valid nodes } /* ============================================= The remove(x) method Note: I made the method return the value stored in the Node object ============================================= */ public double remove( ) throws Exception { /* ============================================== Can't delete from an empty list: error ============================================== */ if ( head == null ) throw new Exception("Remove from empty list"); /* ================================================= Here we delete the node at the tail of the list ================================================= */ double r; if ( head.next == null ) // List has 1 Node { r = head.value; // Save value to return it... head = null; // Mark list as "empty" } else { // Find the last *2* Nodes in the list /* ------------------------------------------------ Initial state: head ---> Node1 ---> Node2 ---> Node3 --->... ^ ^ | | ptr1 ptr ------------------------------------------------- */ Node ptr, ptr1; // ptr1 always points to the Node // before the one pointed to by ptr ptr1 = head; // ptr1 points to first node ptr = head.next; // ptr points to node following ptr1 while ( ptr.next != null ) { ptr1 = ptr; // Move to next pair of node ptr = ptr.next; } // ****************************************************** // ptr NOW points to the last Node in list // ptr1 NOW points to the Node BEFORE the last Node // ****************************************************** r = ptr.value; // Save the the value in last Node // (in case you want to return it) ptr1.next = null; // Mark the // one-before-last node as // the new last node } } } ```

• How to use the method:

 ``` public static void main( String[] args ) { List myList1 = new List(); // Create 2 empty lists List myList2 = new List(); double x; x = myList1.remove(); // Delete in first list x = myList2.remove(); // Delete in second list } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link(s) and save in a scratch directory To compile:   javac testProg2.java To run:          java testProg2