Delete a linked list using recursion

Delete the given linked list using recursion

Method
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
  
/* 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");
    deleteList(head);
    printf("\nLinked list deleted");
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to recursively delete a linked list
class GFG 
{
  
/* Link list node */
static class Node 
{
    int data;
    Node next;
};
  
/* Recursive Function to delete
the entire linked list */
static void deleteList(Node head)
{
    if (head == null)
        return;
    deleteList(head.next); 
    System.gc();
}
  
/* 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. */
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 conbelow 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 */

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to recursively delete a linked list
using System;
  
class GFG 
{
  
/* Link list node */
public class Node 
{
    public int data;
    public Node next;
};
  
/* Recursive Function to delete
the entire linked list */
static void deleteList(Node head)
{
    if (head == null)
        return;
    deleteList(head.next); 
}
  
/* 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. */
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 conbelow 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

chevron_right


Output:

Deleting linked list
Linked list deleted


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : princiraj1992, Rajput-Ji



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.