Description: You are given a reference to the head pointer of a linked list(struct node** head_ref). Your task is to delete the entire linked list.

 

If video does not load, Click here

ALGORITHM:

·         Initialize a current pointer and a previous pointer

·         Let current pointer point to head node

·         Repeat until current does not point to Null

–      previous = current

–      current = current->next

–      free(previous)

 

C IMPLEMENTATION:

 
/* Linked list Node */
struct node
{
    int data;
    struct node* next;
};
 
/*Function to delete the entire linked list */
void delete List(struct node** head_ref)
{
/* Initialize a current pointer and a previous pointer & Let current pointer point to head node */
   struct node* current = *head_ref;
   struct node* previous;
/* Repeat until current does not point to Null */
   while(current != NULL)
   {
previous = current;   /* previous = current */
current=current->next;  /* current=current->next */
free(previous);  /* free previous  */
}  

 

Time Complexity:

  O(n)

Space Complexity:

O(1)

Go to top