Delete a linked list using recursion - GeeksforGeeks

17 Feb, 2022
Delete the given linked list using recursionMethod 1) If head equal to NULL then linked list is empty, we simply return. 2) Recursively delete linked list after head node. 3) Delete head node.  
C++
Java
Python3
C#
Javascript
// C++ program to recursively delete a linked list#include <bits/stdc++.h> /* Link list node */struct Node {    int data;    struct Node* next;}; /* Recursive Function to delete the entire linked list */void deleteList(struct Node* head){    if (head == NULL)        return;    deleteList(head->next);    free(head);} /* 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){    struct Node* new_node = new Node;    new_node->data = new_data;    new_node->next = (*head_ref);    (*head_ref) = new_node;} /* Driver 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");    deleteList(head);         // Since head now points to illgal address, we should set head = NULL:      head = NULL;    printf("\nLinked list deleted");    return 0;}
// Java program to recursively delete a linked listclass GFG{ /* Link list node */static class Node{    int data;    Node next;}; /* Recursive Function to deletethe entire linked list */static void deleteList(Node head){    if (head == null)        return;    deleteList(head.next);    System.gc();} /* Given a reference (pointer to pointer) tothe head of a list and an int, push a newnode on the front of the list. */static void push(Node head_ref, int new_data){    Node new_node = new Node();    new_node.data = new_data;    new_node.next = head_ref;    head_ref = new_node;} /* Driver code*/public static void main(String[] args){    /* Start with the empty list */    Node head = new Node();     /* 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);    System.out.print("\nDeleting linked list");    deleteList(head);    System.out.print("\nLinked list deleted");}} /* This code contributed by PrinciRaj1992 */
# Python3 program to recursively delete# a linked listimport math # Link list nodeclass Node:    def __init__(self, data):        self.data = data        self.next = None # Recursive Function to delete# the entire linked listdef deleteList(head):    if (head == None):        return    deleteList(head.next)    # free(head) # Given a reference (pointer to pointer) to the head# of a list and an int, head=push a new node# on the front of the list.def push(head_ref, new_data):    new_node = Node(new_data)    new_node.data = new_data    new_node.next = head_ref    head_ref = new_node    return head_ref # Driver Codeif __name__=='__main__':         # Start with the empty list    head = None     # Use head=push() to construct below list 1.12.1.4.1    head = push(head, 1)    head = push(head, 4)    head = push(head, 1)    head = push(head, 12)    head = push(head, 1)    print("Deleting linked list")    deleteList(head)    print("Linked list deleted")     # This code is contributed by Srathore
// C# program to recursively delete a linked listusing System; class GFG{ /* Link list node */public class Node{    public int data;    public Node next;}; /* Recursive Function to deletethe entire linked list */static void deleteList(Node head){    if (head == null)        return;    deleteList(head.next);} /* Given a reference (pointer to pointer) tothe head of a list and an int, push a newnode on the front of the list. */static void push(Node head_ref, int new_data){    Node new_node = new Node();    new_node.data = new_data;    new_node.next = head_ref;    head_ref = new_node;} /* Driver code*/public static void Main(String[] args){    /* Start with the empty list */    Node head = new Node();     /* 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);    Console.Write("\nDeleting linked list");    deleteList(head);    Console.Write("\nLinked list deleted");}} // This code contributed by Rajput-Ji
<script> // JavaScript program to recursively delete a linked list /* Link list node */class Node{    constructor()    {        this.data = 0;        this.next = null;    }}; /* Recursive Function to deletethe entire linked list */function deleteList(head){    if (head == null)        return;    deleteList(head.next);} /* Given a reference (pointer to pointer) tothe head of a list and an int, push a newnode on the front of the list. */function push(head_ref, new_data){    var new_node = new Node();    new_node.data = new_data;    new_node.next = head_ref;    head_ref = new_node;} /* Driver code*//* Start with the empty list */var head = new Node();/* Use push() to construct below list1->12->1->4->1 */push(head, 1);push(head, 4);push(head, 1);push(head, 12);push(head, 1);document.write("Deleting linked list");deleteList(head);document.write("<br>Linked list deleted");  </script>
Output:  
Deleting linked list
Linked list deleted
 
YouTubeGeeksforGeeks502K subscribersDelete a linked list using recursion | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 3:45•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=4fyh_ddRyRE" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
?list=PLqM7alHXFySH41ZxzrPNj2pAYPOI8ITe7 
princiraj1992
Rajput-Ji
sapnasingh4991
nidhi_biet
noob2000
satviksingh600
simmytarika5
Linked List
Recursion
Linked List
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
LinkedList in Java
Doubly Linked List | Set 1 (Introduction and Insertion)
Linked List vs Array
Merge two sorted linked lists
Implementing a Linked List in Java using Class
Write a program to print all permutations of a given string
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Recursion
Program for Tower of Hanoi
Program for Sum of the digits of a given number