Write a function to delete a Linked List
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.
Table of Content
[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++ 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 program to delete a linked list
// using recursion
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Given the head of a list, delete the list
// using recursion
void deleteList(struct Node* curr) {
// Base case: If the list is empty, return
if (curr == NULL) {
return;
}
// Recursively delete the next node
deleteList(curr->next);
// Delete the current node
free(curr);
}
struct Node* createNode(int new_data) {
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
int main() {
// Create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
deleteList(head);
printf("NULL");
return 0;
}
// Java program to delete a linked list
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
public class GfG {
public static void main(String[] args) {
// 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);
// Set head to null to remove the reference to the linked list.
// This allows Java's garbage collector to automatically clean up
// the memory used by the nodes, as there are no more references
// to the nodes in the linked list.
head = null;
System.out.print("NULL");
}
}
# Python program to delete a linked list
class Node:
def __init__(self, x):
self.data = x
self.next = None
if __name__ == "__main__":
# Create a hard-coded linked list:
# 1 -> 2 -> 3 -> 4 -> 5
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
# Set head to None to remove the reference to the linked list.
# This allows Python's garbage collector to automatically reclaim
# the memory used by the nodes, as there are no more references
# to the nodes in the linked list.
head = None
print("NULL")
// C# program to delete a linked list
using System;
class Node {
public int Data;
public Node next;
public Node(int x) {
Data = x;
next = null;
}
}
class GfG {
static void 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);
// Set head to null to remove the reference to the linked list.
// This allows C#'s garbage collector to automatically reclaim
// the memory used by the nodes, as there are no more references
// to the nodes in the linked list.
head = null;
Console.WriteLine("NULL");
}
}
// JavaScript program to delete a linked list
class Node {
constructor(x) {
this.data = x;
this.next = null;
}
}
// Create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5
let 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);
// Set head to null to remove the reference to the linked list.
// This allows JavaScript's garbage collector to automatically reclaim
// the memory used by the nodes, as there are no more references
// to the nodes in the linked list.
head = null;
console.log("NULL");
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++ 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 program to delete a linked list
// using iteration
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Given the head of a list, delete the list
// using iteration
void deleteList(struct Node* curr) {
// Iterate through the list until it becomes empty
while (curr != NULL) {
// Store the next node before deleting current
struct Node* temp = curr->next;
// Delete the current node
free(curr);
// Move to the next node
curr = temp;
}
}
struct Node* createNode(int new_data) {
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
int main() {
// Create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
deleteList(head);
head = NULL;
printf("NULL");
return 0;
}
// Java program to delete a linked list
class Node {
int data;
Node next;
Node(int x) {
data = x;
next = null;
}
}
public class GfG {
public static void main(String[] args) {
// 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);
// Set head to null to remove the reference to the linked list.
// This allows Java's garbage collector to automatically clean up
// the memory used by the nodes, as there are no more references
// to the nodes in the linked list.
head = null;
System.out.print("NULL");
}
}
# Python program to delete a linked list
class Node:
def __init__(self, x):
self.data = x
self.next = None
if __name__ == "__main__":
# Create a hard-coded linked list:
# 1 -> 2 -> 3 -> 4 -> 5
head = Node(1)
head.next = Node(2)
head.next.next = Node(3)
head.next.next.next = Node(4)
head.next.next.next.next = Node(5)
# Set head to None to remove the reference to the linked list.
# This allows Python's garbage collector to automatically reclaim
# the memory used by the nodes, as there are no more references
# to the nodes in the linked list.
head = None
print("NULL")
// C# program to delete a linked list
using System;
class Node {
public int Data;
public Node next;
public Node(int x) {
Data = x;
next = null;
}
}
class GfG {
static void 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);
// Set head to null to remove the reference to the linked list.
// This allows C#'s garbage collector to automatically reclaim
// the memory used by the nodes, as there are no more references
// to the nodes in the linked list.
head = null;
Console.WriteLine("NULL");
}
}
// JavaScript program to delete a linked list
class Node {
constructor(x) {
this.data = x;
this.next = null;
}
}
// Create a hard-coded linked list:
// 1 -> 2 -> 3 -> 4 -> 5
let 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);
// Set head to null to remove the reference to the linked list.
// This allows JavaScript's garbage collector to automatically reclaim
// the memory used by the nodes, as there are no more references
// to the nodes in the linked list.
head = null;
console.log("NULL");
Output
NULL
Time Complexity: O(n), where n is the number of nodes in the given linked list.
Auxiliary Space: O(1)