Recursive insertion and traversal linked list

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


 

Recursively traversing the list:
The idea is simple, we print current node and recur for remaining list.

filter_none

edit
close

play_arrow

link
brightness_4
code

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);
}

chevron_right


 

Complete Program:
Below is complete program to demonstrate working of insert and traverse a linked list.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Recursive CPP 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);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Recursive Java program to recursively insert 
// a node and recursively print the list. 
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); 
}
  
// This code is contributed by andrew1234

chevron_right


C#

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


Output:

6 8 10 12 14

This article is contributed by AMIT KUMAR. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : andrew1234, 29AjayKumar



Article Tags :
Practice Tags :


Be the First to upvote.


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