Delete a linked list using recursion
Last Updated :
31 Jan, 2023
Delete the given linked list using recursion
Method:
- If head equal to NULL then linked list is empty, we simply return.
- Recursively delete linked list after head node.
- Delete head node.
Implementation:
C++
#include <bits/stdc++.h>
struct Node {
int data;
struct Node* next;
};
void deleteList( struct Node* head)
{
if (head == NULL)
return ;
deleteList(head->next);
free (head);
}
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;
}
int main()
{
struct Node* head = NULL;
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
printf ( "\n Deleting linked list" );
deleteList(head);
head = NULL;
printf ( "\nLinked list deleted" );
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static void deleteList(Node head)
{
if (head == null )
return ;
deleteList(head.next);
System.gc();
}
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;
}
public static void main(String[] args)
{
Node head = new Node();
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" );
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def deleteList(head):
if (head = = None ):
return
deleteList(head. next )
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
if __name__ = = '__main__' :
head = None
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" )
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static void deleteList(Node head)
{
if (head == null )
return ;
deleteList(head.next);
}
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;
}
public static void Main(String[] args)
{
Node head = new Node();
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" );
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
};
function deleteList(head)
{
if (head == null )
return ;
deleteList(head.next);
}
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;
}
var head = new Node();
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.
Share your thoughts in the comments
Please Login to comment...