The Wayback Machine - https://web.archive.org/web/20240717181823/https://www.geeksforgeeks.org/delete-linked-list-using-recursion/
Open In App

Delete a linked list using recursion

Last Updated : 31 Jan, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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. 

Implementation:

C++




// 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




// 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 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




# Python3 program to recursively delete
# a linked list
import math
 
# Link list node
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None
 
# Recursive Function to delete
# the entire linked list
def 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 Code
if __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#




// 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 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


Javascript




<script>
 
// JavaScript program to recursively delete a linked list
 
/* Link list node */
class Node
{
    constructor()
    {
        this.data = 0;
        this.next = null;
    }
};
 
/* Recursive Function to delete
the entire linked list */
function deleteList(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. */
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 list
1->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

Time Complexity: O(N), where N is the number of nodes in the given linked list.
Auxiliary Space: O(N) due to recursion call stack.



Previous Article
Next Article

Similar Reads

Why is Tail Recursion optimization faster than normal Recursion?
What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recursive function in which the recursive call is the f
4 min read
Delete nodes which have a greater value on right side using recursion
Given a singly linked list, remove all the nodes which have a greater value on the right side. Examples: a) The list 12-&gt;15-&gt;10-&gt;11-&gt;5-&gt;6-&gt;2-&gt;3-&gt;NULL should be changed to 15-&gt;11-&gt;6-&gt;3-&gt;NULL. Note that 12, 10, 5 and 2 have been deleted because there is a greater value on the right side. When we examine 12, we see
7 min read
Remove duplicates from a sorted linked list using recursion
Write a removeDuplicates() function which takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once. For example if the linked list is 11-&gt;11-&gt;11-&gt;21-&gt;43-&gt;43-&gt;60 then removeDuplicates() should convert the list to 11-&gt;21-&gt;43-&gt;60. Algorithm: Traverse th
8 min read
Reverse a Doubly linked list using recursion
Given a doubly linked list. Reverse it using recursion. Original Doubly linked list Reversed Doubly linked list We have discussed Iterative solution to reverse a Doubly Linked List Algorithm: If list is empty, return Reverse head by swapping head-&gt;prev and head-&gt;next If prev = NULL it means that list is fully reversed. Else reverse(head-&gt;p
9 min read
Print alternate nodes of a linked list using recursion
Given a linked list, print alternate nodes of this linked list. Examples : Input : 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; 7 -&gt; 8 -&gt; 9 -&gt; 10 Output : 1 -&gt; 3 -&gt; 5 -&gt; 7 -&gt; 9 Input : 10 -&gt; 9 Output : 10 Recursive Approach : Initialize a static variable(say flag) If flag is odd print the node increase head and flag by 1,
6 min read
Traverse Linked List from middle to left-right order using recursion
Given a Linked List. The task is to traverse the Linked List from middle to left-right order using recursion.For Example: If the given Linked List is: 2 -&gt; 5 -&gt; 8 -&gt; 3 -&gt; 7 -&gt; 9 -&gt; 12 -&gt; NULL The Middle to left-right order is : 3, 8, 7, 5, 9, 2, 12 Explanation: Middle of the given linked list is '3' so, we start traversing from
15+ min read
Add the given digit to a number stored in a linked list using recursion
Given a linked list which represents an integer number where each node is a digit of the represented integer. The task is to add a given digit N to the represented integer.Examples: Input: LL = 9 -&gt; 9 -&gt; 3 -&gt; NULL, N = 7 Output: 1 -&gt; 0 -&gt; 0 -&gt; 0 -&gt; NULL 993 + 7 = 1000Input: LL = 2 -&gt; 9 -&gt; 9 -&gt; NULL, N = 5 Output: 3 -
10 min read
How to insert a Node in a Singly Linked List at a given Position using Recursion
Given a singly linked list as list, a position, and a node, the task is to insert that element in the given linked list at a given position using recursion. Examples: Input: list = 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7, node = (val=100,next=null), position = 4 Output: 1-&gt;2-&gt;3-&gt;100-&gt;4-&gt;5-&gt;6-&gt;7Explanation: Here the node with valu
14 min read
Reverse a linked list using recursion
Given pointer to the head node of a linked list, the task is to recursively reverse the linked list. We need to reverse the list by changing links between nodes.  Examples:  Input: Head: 1-&gt;2-&gt;3-&gt;4-&gt;NULLOutput: Reversed Linked list : 4-&gt;3-&gt;2-&gt;1-&gt;NULL Input: Head: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULLOutput: Reversed Linked list
12 min read
Practice questions for Linked List and Recursion
Assume the structure of a Linked List node is as follows. C/C++ Code struct Node { int data; struct Node *next; }; // This code is contributed by SHUBHAMSINGH10 C/C++ Code struct Node { int data; struct Node *next; }; Java Code static class Node { int data; Node next; }; // This code is contributed by shubhamsingh10 C/C++ Code class Node: def __ini
11 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg