Delete a linked list using recursion (original) (raw)
Last Updated : 30 Aug, 2024
Given a **linked list, the task is to delete the linked list using **recursion.
**Examples:
**Input: Linked List = 1 -> 2 -> 3 -> 4 -> 5 -> NULL
**Output: NULL
**Explanation: Linked List is Deleted**Input: Linked List = 1 -> 12 -> 1 -> 4 -> 1 -> NULL
**Output: NULL
**Explanation: Linked List is Deleted
**Approach :
The idea is to use **recursion to traverse the linked list until we reach to **end. While **backtracking we keep on removing the **current node in the linked list.
Below is the implementation of the above approach:
C++ `
// C++ program to delete a linked list // using recursion
#include using namespace std;
class Node { public: int data; Node* next;
Node(int new_data) {
data = new_data;
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);
head = nullptr;
cout << "Linked List deleted.";
return 0;
}
C
// 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);
head = NULL;
printf("Linked List deleted.");
return 0;
}
Java
// Java program to delete a linked list // using recursion class Node { int data; Node next;
Node(int new_data) {
data = new_data;
next = null;
}
}
// Given the head of a list, delete the list // using recursion public class GfG { static void deleteList(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
curr = null;
}
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);
deleteList(head);
head = null;
System.out.print("Linked List deleted.");
}
}
Python
Python program to delete a linked list
using recursion
class Node: def init(self, new_data): self.data = new_data self.next = None
Given the head of a list, delete the list
using recursion
def delete_list(curr):
# Base case: If the list is empty, return
if curr is None:
return
# Recursively delete the next node
delete_list(curr.next)
# Delete the current node
curr = 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)
delete_list(head)
head = None
print("Linked List deleted.")
C#
// C# program to delete a linked list // using recursion using System;
class Node { public int Data; public Node next;
public Node(int newData) {
Data = newData;
next = null;
}
}
// Given the head of a list, delete the list // using recursion class GfG { static void DeleteList(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
curr = null;
}
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);
DeleteList(head);
head = null;
Console.WriteLine("Linked List deleted.");
}
}
JavaScript
// JavaScript program to delete a // linked list using recursion class Node { constructor(newData) { this.data = newData; this.next = null; } }
// Given the head of a list, delete the list // using recursion function deleteList(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
curr = 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);
deleteList(head); head = null; console.log("Linked List deleted.");
`
Output
Linked List deleted.
**Time Complexity: O(n), where n is the number of nodes in the given linked list.
**Auxiliary Space: O(n)