Deleting a list element at the tail of a linked list
 

My problem:

 

Deleting a list element at the tail of a linked list
 

The solution of my problem is:

We need to find an (recursive) algorithm that solves My Problem.

Designing a recursive algorithm to delete at tail
 

Consider the smaller problem delete(head->next):

 

Designing a recursive algorithm to delete at tail
 

The solution of delete(head.next) is:

 

Designing a recursive algorithm to delete at tail
 

Suppose we have the solution of delete(head.next) avialable:

How can we use this solution to solve our original problem ?

Designing a recursive algorithm to delete at tail
 

Answer:

So:   what is the (recursive) algorithm ?

Designing a recursive algorithm to delete at tail
 

The recursive algorithm:

helpSol = delete( h->next ) ; --- ask someone else to solve this smaller problem
 

Designing a recursive algorithm to delete at tail
 

The recursive algorithm:

helpSol = delete( h->next ) ; --- ask someone else to solve this smaller problem
head->next = helpSol ; --- use the smaller solution to solve my problem !

Base case #1 for delete at tail

Base case #1:   delete in an empty list

Solution:

Algorithm:
             return NULL;

Base case #2 for delete at tail

Base case #1:   delete the last element from a list

Solution:

Algorithm:
             free(head) ; --- we must de-allocate the deleted element ourselves !
             return NULL;

Recursive algorithm to delete at tail of a linked list

   struct List *delete( struct List *head, struct List *e )
   {
      struct List * helpSol;

      /* --------------------------------------------
          Base case: delete at the tail of an empty
         -------------------------------------------- */
      if ( head == NULL )
      {
         return(NULL);   // Empty list
      }
      else if ( head->next == NULL )
      {
         free(head) ;    // De-allocate deleted element
	 return(NULL);   // Empty list
      }
      else
      {
         /* ===========================================================
            Solve the problem USING the solution of a smaller problem
            =========================================================== */

	    
         helpSol = delete( head->next );   // Have "someone else" solve
                                           // this smaller problem

	    
         head->next = helpSol;    // Find MY solution using helpSol
         return head;             // Return MY solution
      }
   } 

DEMO:   /home/cs255001/demo/C/Recursion/delete-tail.c

Simplified recursive algorithm to delete at tail of a linked list

   struct List *delete( struct List *head, struct List *e )
   {
      /* --------------------------------------------
          Base case: delete at the tail of an empty
         -------------------------------------------- */
      if ( head == NULL )
      {
         return(NULL);   // Empty list
      }
      else if ( head->next == NULL )
      {
         free(head) ;    // De-allocate deleted element
	 return(NULL);   // Empty list
      }
      else
      {
         /* ===========================================================
            Solve the problem USING the solution of a smaller problem
            =========================================================== */
         head->next = delete( head->next ); // Link directly to helpSol

         return head;             // Return MY solution
      }
   }