### Using recursion to delete the node at the tail of a list

• Deleting a node at the tail of a list using recursion

• Example:

• This is my problem:

• The solution is:

• The methodology that you need to use is:

• Take advantage of the solution of this smaller problem:

• Smaller problem:

• The solution of the smaller problem is:

• Question: how can I use the solution of the smaller problem to solve my original problem

Depicted graphically:

Answer: I can achieve it using this statement:

 ``` head.next = smallerSol; ```

• Pseudo code for the recursive solution:

 ``` smallerSol = delete( head.next ); // Have someone else delete the last // node in a SHORTER list and return // back the new list head.next = smallerSol; // Use the smaller solution to // solve the original problem solution = head; // The solution is the list starting // at head return solution; // Return the solution ```

(I will put the pseudo code inside the recursive method later)

• Find the base case(s)

• When is it easy to delete the last node from a linked list:

1. When the list is empty

 There is no last node, so you just return the empty list as answer

How to detect this case:

 ``` if ( head == null ) ```

2. When the list contains only one node

 Deleting the last node from a list will result in an empty list. You also return the empty list as answer

How to detect this case:

 ``` if ( head.next == null ) ```

• The recursive delete algorithm: delete the node at the tail of a list

• Pseudo code:

 ``` /* ============================ Base cases ============================ */ if ( list is empty ) // Base case 1 { solution = empty list; return solution; } else ( list contains 1 node ) // Base case 2 { solution = empty list; return solution; } else { smallerSol = delete the last node from the list starting at head.next head.next = smallerSol; // Solve problem with smaller solution solution = list starting at head; return solution; } ```

• Java:

 ``` public static Node delete( Node head ) { Node solution, smallerSol; /* ============================ Base cases ============================ */ if ( head == null ) // Base case 1 { solution = null; return solution; } else if ( head.next == null ) // Base case 2 { solution = null; return solution; } else { /* ================================================= I know the list is not empty... And "head.next" points to a SHORTER list ! ================================================= */ smallerSol = delete( head.next ); // Have someone else delete the last // node in a SHORTER list and return // back the new list head.next = smallerSol; // Solve problem with smaller solution solution = head; // Solution = list starting at head return solution; // Return the head of the new list } } ```

• How to use the delete method:

 ``` public static void main( String[] args ) { Node head = null; Node e; double[] a = {4, 1, 3, 7}; for ( int i = 0; i < a.length; i++) { e = new Node(a[i]); head = RecursiveList.insert(head, e); } RecursiveList.printList( head ); for ( int i = 0; i < a.length; i++) { head = RecursiveList.delete(head); // delete() returns the new list RecursiveList.printList( head ); } } ```

Output:

 ``` [4.0 , 1.0 , 3.0 , 7.0] [4.0 , 1.0 , 3.0] - tail node deleted ! [4.0 , 1.0] [4.0] [] ```

• 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