We have discussed different methods of linked list insertion. How to recursively create a linked list?
Recursively inserting at the end: To create a Linked list using recursion follow these steps. Below steps insert a new node recursively at the end of linked list.
// Function to insert a new node at the // end of linked list using recursion. Node* insertEnd(Node* head, int data)
{ // If linked list is empty, create a
// new node (Assuming newNode() allocates
// a new node with given data)
if (head == NULL)
return newNode(data);
// If we have not reached end, keep traversing
// recursively.
else
head->next = insertEnd(head->next, data);
return head;
} |
// Function to insert a new node at the // end of linked list using recursion. static Node insertEnd(Node head, int data)
{ // If linked list is empty, create a
// new node (Assuming newNode() allocates
// a new node with given data)
if (head == null )
return newNode(data);
// If we have not reached end, keep traversing
// recursively.
else
head.next = insertEnd(head.next, data);
return head;
} // This code is contributed by rutvik_56 |
# Function to insert a new node at the # end of linked list using recursion. def insertEnd(head, data):
# If linked list is empty, create a
# new node (Assuming newNode() allocates
# a new node with given data)
if (head = = None ):
return newNode(data)
# If we have not reached end,
# keep traversing recursively.
else :
head. next = insertEnd(head. next , data)
return head
# This code is contributed by divyeshrabadiya07 |
// Function to insert a new node at the // end of linked list using recursion. static Node insertEnd(Node head, int data)
{ // If linked list is empty, create a
// new node (Assuming newNode() allocates
// a new node with given data)
if (head == null )
return newNode(data);
// If we have not reached end, keep traversing
// recursively.
else
head.next = insertEnd(head.next, data);
return head;
} // This code is contributed by divyesh072019 |
<script> // Function to insert a new node at the // end of linked list using recursion. function insertEnd(head , data)
{ // If linked list is empty, create a
// new node (Assuming newNode() allocates
// a new node with given data)
if (head == null )
{
return newNode(data);
}
// If we have not reached end, keep traversing
// recursively.
else
{
head.next = insertEnd(head.next, data);
}
return head;
} // This code is contributed by shubhamsingh10 </script> |
Time Complexity: O(N) where N is the number of nodes in given linked list.
Auxiliary Space: O(N) due to recursion call stack.
Recursively traversing the list:
The idea is simple, we print current node and recur for remaining list.
Implementation:
void traverse(Node* head)
{ if (head == NULL)
return ;
// If head is not NULL, print current node
// and recur for remaining list
cout << head->data << " " ;
traverse(head->next);
} |
static void traverse(Node head)
{ if (head == null )
return ;
// If head is not null, print current node
// and recur for remaining list
System.out.print( head.data + " " );
traverse(head.next);
} // This code is contributed by SUBHAMSINGH10 |
def traverse(head):
if (head = = None ):
return
# If head is not None, print current node
# and recur for remaining list
print (head.data, end = " " );
traverse(head. next )
# This code is contributed by Pratham76
|
static void traverse(Node head)
{ if (head == null )
return ;
// If head is not null, print current node
// and recur for remaining list
Console.Write(head.data + " " );
traverse(head.next);
} |
function traverse(head){
if (head == null )
return ;
// If head is not NULL, print current node
// and recur for remaining list
console.log(head.data + " " );
traverse(head.next);
} // This code is contributed by Yash Agarwal(yashagarwal2852002) |
Complete Program: Below is complete program to demonstrate working of insert and traverse a linked list.
// Recursive C++ program to recursively insert // a node and recursively print the list. #include <bits/stdc++.h> using namespace std;
struct Node {
int data;
Node* next;
}; // Allocates a new node with given data Node *newNode( int data)
{ Node *new_node = new Node;
new_node->data = data;
new_node->next = NULL;
return new_node;
} // Function to insert a new node at the // end of linked list using recursion. Node* insertEnd(Node* head, int data)
{ // If linked list is empty, create a
// new node (Assuming newNode() allocates
// a new node with given data)
if (head == NULL)
return newNode(data);
// If we have not reached end, keep traversing
// recursively.
else
head->next = insertEnd(head->next, data);
return head;
} void traverse(Node* head)
{ if (head == NULL)
return ;
// If head is not NULL, print current node
// and recur for remaining list
cout << head->data << " " ;
traverse(head->next);
} // Driver code int main()
{ Node* head = NULL;
head = insertEnd(head, 6);
head = insertEnd(head, 8);
head = insertEnd(head, 10);
head = insertEnd(head, 12);
head = insertEnd(head, 14);
traverse(head);
} |
// THIS CODE IS CONTRIBUTED BY KIRTI AGARWAL(KIRTIAGARWAL23121999) // Recursive Java program to recursively insert // a node and recursively print the list. public class GFG {
static class Node {
int data;
Node next;
};
// Allocates a new node with given data
static Node newNode( int data){
Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
}
// Function to insert a new node at the
// end of linked list using recursion.
static Node insertEnd(Node head, int data){
// If linked list is empty, create a
// new node (Assuming newNode() allocates
// a new node with given data)
if (head == null )
return newNode(data);
// If we have not reached end, keep traversing
// recursively.
else
head.next = insertEnd(head.next, data);
return head;
}
static void traverse(Node head){
if (head == null )
return ;
// If head is not null, print current node
// and recur for remaining list
System.out.print(head.data + " " );
traverse(head.next);
}
// Driver code
public static void main(String args[]){
Node head = null ;
head = insertEnd(head, 6 );
head = insertEnd(head, 8 );
head = insertEnd(head, 10 );
head = insertEnd(head, 12 );
head = insertEnd(head, 14 );
traverse(head);
}
} |
# Recursive Python3 program to # recursively insert a node and # recursively print the list. import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
# Allocates a new node with given data def newNode(data):
new_node = Node(data)
new_node.data = data
new_node. next = None
return new_node
# Function to insert a new node at the # end of linked list using recursion. def insertEnd(head, data):
# If linked list is empty, create a
# new node (Assuming newNode() allocates
# a new node with given data)
if (head = = None ):
return newNode(data)
# If we have not reached end,
# keep traversing recursively.
else :
head. next = insertEnd(head. next , data)
return head
def traverse(head):
if (head = = None ):
return
# If head is not None, print current node
# and recur for remaining list
print (head.data, end = " " );
traverse(head. next )
# Driver code if __name__ = = '__main__' :
head = None
head = insertEnd(head, 6 )
head = insertEnd(head, 8 )
head = insertEnd(head, 10 )
head = insertEnd(head, 12 )
head = insertEnd(head, 14 )
traverse(head)
# This code is contributed by sapna singh |
// Recursive C# program to recursively insert // a node and recursively print the list. using System;
class GFG
{ public class Node
{ public int data;
public Node next;
}; // Allocates a new node with given data static Node newNode( int data)
{ Node new_node = new Node();
new_node.data = data;
new_node.next = null ;
return new_node;
} // Function to insert a new node at the // end of linked list using recursion. static Node insertEnd(Node head, int data)
{ // If linked list is empty, create a
// new node (Assuming newNode() allocates
// a new node with given data)
if (head == null )
return newNode(data);
// If we have not reached end, keep traversing
// recursively.
else
head.next = insertEnd(head.next, data);
return head;
} static void traverse(Node head)
{ if (head == null )
return ;
// If head is not null, print current node
// and recur for remaining list
Console.Write(head.data + " " );
traverse(head.next);
} // Driver code public static void Main(String []args)
{ Node head = null ;
head = insertEnd(head, 6);
head = insertEnd(head, 8);
head = insertEnd(head, 10);
head = insertEnd(head, 12);
head = insertEnd(head, 14);
traverse(head);
} } // This code is contributed by 29AjayKumar |
<script> // Recursive Javascript program to recursively insert
// a node and recursively print the list.
class Node
{
constructor(data) {
this .next = null ;
this .data = data;
}
}
// Allocates a new node with given data
function newNode(data)
{
let new_node = new Node(data);
return new_node;
}
// Function to insert a new node at the
// end of linked list using recursion.
function insertEnd(head, data)
{
// If linked list is empty, create a
// new node (Assuming newNode() allocates
// a new node with given data)
if (head == null )
return newNode(data);
// If we have not reached end, keep traversing
// recursively.
else
head.next = insertEnd(head.next, data);
return head;
}
function traverse(head)
{
if (head == null )
return ;
// If head is not null, print current node
// and recur for remaining list
document.write(head.data + " " );
traverse(head.next);
}
let head = null ;
head = insertEnd(head, 6);
head = insertEnd(head, 8);
head = insertEnd(head, 10);
head = insertEnd(head, 12);
head = insertEnd(head, 14);
traverse(head);
// This code is contributed by mukesh07. </script> |
6 8 10 12 14
Time Complexity: O(N), to traverse the linked list of size N.
Auxiliary Space: O(N), for recursion call stack