Delete Complete Linked List

Delete Complete Linked List” is one of the basic problem exercise for beginners. Here, we are given a linked list and our task is to delete complete linked list or simply, delete all the nodes of the linked list.

To do so, we will traverse the complete linked list and then delete the given nodes one after another during traversing.

The steps required to delete complete linked list is as follows:

  1. Traverse the complete linked list.
  2. While traversing, delete the nodes one after another.
  3. Mark head of the list as NULL.

C++ Program to delete complete linked list is as follows:

/* C++ Program to Delete Complete Linked List */
#include<bits/stdc++.h>
using namespace std;

/* strcuture of the node of the Linked List */
struct node
{
    int data;
    struct node *next;
};

/* Function to inset node at the end of the linked list */
void insert_end(struct node **head, int ele)
{
    /* Creating a new node */
    struct node *new_node;
    new_node = (struct node*)malloc(sizeof(struct node));
    new_node->data = ele;
    new_node->next = NULL;
     
    /* check whether the linked list is empty or not */
    
    /* If Linked List is empty, make new node to the head of the linked list */
    if(*head == NULL)     
    {
        *head = new_node;
        return;
    }
    
    /* If Linked List is not empty, then traverse the complete linked list and point the pointer
    of the last node of the linked list to the new node */
    struct node *temp = NULL;
    temp = (*head);
    while(temp->next != NULL)
    temp=temp->next;
    temp->next = new_node;
}

/* Function to delete the complete Linked List */
void deleteALL(struct node **head)
{
    /* If Linked List is NULL, simply return */
    if(*head == NULL)
    return;
    struct node *temp = (*head);
    struct node *p;
    /* Traverse the complete Linked List and Delete all the nodes one by one */
    while(temp!=NULL)
    {
        p = temp;
        temp = temp->next;
        delete p;
    }
    /* Make Head to the NULL */
    (*head) = NULL;
}

/* Function to Print the Linked List */
void print(struct node *head)
{
    struct node *p = head;
    while(p!=NULL)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
}
int main()
{
    struct node *head;
    head = NULL;
    /* Inserting Some Nodes in the Linked List */
    insert_end(&head,1);
    insert_end(&head,2);
    insert_end(&head,3);
    insert_end(&head,4);
    cout<<"The linked list elements before deleting:\n";
    print(head);
    deleteALL(&head);
    cout<<"\nThe linked list elements after deleting:\n";
    print(head);
}
OUTPUT:
The linked list elements before deleting:
1 2 3 4
The linked list elements after deleting:

Related Posts:

  1. Delete Nth Node of the Linked List.
  2. Delete without head pointer of the Linked List.
  3. Delete All Occurrences of particular node of the Linked List.
  4. Delete Alternate Nodes of the Linked List.
  5. Delete every ‘N’ Nodes After ‘M’ Nodes of the Linked List.
  6. Remove Duplicate Nodes from an Unsorted Linked List.
  7. Remove Duplicate Nodes from a Sorted Linked List.
  8. Find Union and Intersection of Two Linked List.
  9. Merge Two Sorted Linked List.
  10. Insert a New Node at the Sorted Linked List.
  11. Reverse a Linked List.
  12. Reverse a Linked List Using Stack.
  13. Printing Linked List in Reverse Order without actually reversing the Linked List.
  14. Swap Adjacent Elements of the Linked List.
  15. Count All Occurrences of a Particular Node in a Linked List.
  16. Bubble Sort on Linked List.
  17. Detect a Loop in a Linked List.
  18. Find the Length of the Loop present in the Linked List.
  19. Detect and Remove Loop from a Linked List.
  20. Insert a New Node at the Middle of the Linked List.

You may also like...