Open In App

Write a function to delete a Linked List

Last Updated : 13 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a linked list, the task is to delete the linked list completely.

Examples:

Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> NULL
Output: NULL
Explanation: Linked List is Deleted.

Input: head: 1 -> 12 -> 1 -> 4 -> 1 -> NULL
Output: NULL
Explanation: Linked List is Deleted.

[Expected Approach – 1] Using Recursion – O(n) Time and O(n) Space:

The idea is to use recursion to delete the entire linked list. Traverse from the head to the end of the list recursively. While backtracking, delete the currrent node. This ensures that each node is processed after its subsequent nodes.

Below is the implementation of the above approach:

C++
// C++ program to delete a linked list 
// using recursion
#include <iostream>
using namespace std;

class Node {
public:
    int data;
    Node* next;

    Node(int x) {
        data = x;
        next = nullptr;
    }
};

// Given the head of a list, delete the list
// using recursion
void deleteList(Node* curr) {

    // Base case: If the list is empty, return
    if (curr == nullptr) {
        return;
    }

    // Recursively delete the next node
    deleteList(curr->next);

    // Delete the current node
    delete curr;
}

int main() {

    // Create a hard-coded linked list:
    // 1 -> 2 -> 3 -> 4 -> 5
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);
    head->next->next->next->next = new Node(5);

    deleteList(head); 
    cout << "NULL";

    return 0;
}
C Java Python C# JavaScript

Output
NULL

Time Complexity: O(n), where n is the number of nodes in the given linked list.
Auxiliary Space: O(n)

[Expected Approach – 2] Using Iteration – O(n) Time and O(1) Space:

The idea is to iteratively delete the list by starting from the head and moving towards the end. At each step, the function stores a reference to the next node, deletes the current node, and moves to the next node. This continues until all nodes are deleted.

Below is the implementation of the above approach:

C++
// C++ program to delete a linked list 
// using iteration
#include <iostream>
using namespace std;

class Node {
public:
    int data;
    Node* next;

    Node(int x) {
        data = x;
        next = nullptr;
    }
};

// Given the head of a list, delete the list
// using iteration
void deleteList(Node* curr) {

    // Iterate through the list until it becomes empty
    while (curr != nullptr) {

        // Store the next node before deleting current
        Node* temp = curr->next;

        // Delete the current node
        delete curr;

        // Move to the next node
        curr = temp;
    }
}

int main() {

    // Create a hard-coded linked list:
    // 1 -> 2 -> 3 -> 4 -> 5
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);
    head->next->next->next->next = new Node(5);

    deleteList(head);
    head = nullptr; 
 
    cout << "NULL";

    return 0;
}
C Java Python C# JavaScript

Output
NULL

Time Complexity: O(n), where n is the number of nodes in the given linked list.
Auxiliary Space: O(1)



Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg