Write a function to delete a Linked List

Algorithm For C/C++: Iterate through the linked list and delete all the nodes one by one. Main point here is not to access next of the current pointer if current pointer is deleted.

In Java, automatic garbage collection happens, so deleting a linked list is easy. We just need to change head to null.



// C program to delete a linked list
/* Link list node */
struct Node
    int data;
    struct Node* next;
/* Function to delete the entire linked list */
void deleteList(struct Node** head_ref)
   /* deref head_ref to get the real head */
   struct Node* current = *head_ref;
   struct Node* next;
   while (current != NULL) 
       next = current->next;
       current = next;
   /* deref head_ref to affect the real head back
      in the caller. */
   *head_ref = NULL;
/* Given a reference (pointer to pointer) to the head
  of a list and an int, push a new node on the front
  of the list. */
void push(struct Node** head_ref, int new_data)
    /* allocate node */
    struct Node* new_node =
            (struct Node*) malloc(sizeof(struct Node));
    /* put in the data  */
    new_node->data  = new_data;
    /* link the old list off the new node */
    new_node->next = (*head_ref);
    /* move the head to point to the new node */
    (*head_ref)    = new_node;
/* Drier program to test count function*/
int main()
    /* Start with the empty list */
    struct Node* head = NULL;
    /* Use push() to construct below list
     1->12->1->4->1  */
    push(&head, 1);
    push(&head, 4);
    push(&head, 1); 
    push(&head, 12);
    push(&head, 1);   
    printf("\n Deleting linked list");
    printf("\n Linked list deleted");


// Java program to delete a linked list
class LinkedList
    Node head; // head of the list
    /* Linked List node */
    class Node
        int data;
        Node next;
        Node(int d) { data = d; next = null; }
    /* Function deletes the entire linked list */
    void deleteList()
        head = null;
    /* Inserts a new Node at front of the list. */
    public void push(int new_data)
        /* 1 & 2: Allocate the Node &
                  Put in the data*/
        Node new_node = new Node(new_data);
        /* 3. Make next of new Node as head */
        new_node.next = head;
        /* 4. Move the head to point to new Node */
        head = new_node;
    public static void main(String [] args)
        LinkedList llist = new LinkedList();
        /* Use push() to construct below list
           1->12->1->4->1  */
        System.out.println("Deleting the list");
        System.out.println("Linked list deleted");
// This code is contributed by Rajat Mishra


# Python3 program to delete all 
# the nodes of singly linked list
# Node class
class Node:
    # Function to initialise the node object
    def __init__(self, data):
        self.data = data # Assign data
        self.next = None # Initialize next as null
# Constructor to initialize the node object
class LinkedList:
    # Function to initialize head
    def __init__(self):
        self.head = None
    def deleteList(self):
        # initialize the current node
        current = self.head
        while current:
            prev = current.next # move next node
            # delete the current node
            del current.data
            # set current equals prev node
            current = prev 
    # push function to add node in front of llist
    def push(self, new_data):
        # Allocate the Node &
        # Put in the data
        new_node = Node(new_data)
        # Make next of new Node as head
        new_node.next = self.head
        # Move the head to point to new Node
        self.head = new_node
# Use push() to construct below 
# list 1-> 12-> 1-> 4-> 1  
if __name__ == '__main__':
    llist = LinkedList()
    print("Deleting linked list")
    print("Linked list deleted")
# This article is provided by Shrikant13


 Deleting linked list
 Linked list deleted

Time Complexity: O(n)
Auxiliary Space: O(1)

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up