Recursive insertion and traversal linked list
Last Updated :
29 Aug, 2024
The task is to implement a singly linked list with two core functionalities using recursion:
Insertion: Insert a new node at the end of the linked list.
Traversal: Traverse the linked list and print the data stored in each node.
Example :
Input: Values to be inserted = 1, 2, 3, 4, 5
Output: 1 -> 2 -> 3 -> 4-> 5 -> NULL
Explanation : We have to first recursively insert each node with the given input value and then print the linked list recursively
Input: Values to be inserted = 7, 8, 4, 11, 44, 6
Output: 7 -> 8 -> 4 -> 11 -> 44 -> 6 -> NULL
Approach :
Define a Node class/struct with data and a reference to the next node. Implement a recursive function to insert a new node at the end of the list. Finally, use a recursive traversal function to print the data of each node in the list.
Follow the steps below to solve the problem:
1. Insert a Node: Write a function that adds a new node to the end of the list. If the list is empty, this new node becomes the first node. If the list already has nodes, the function will keep moving to the next node until it finds the last one, then adds the new node there.
2. Traverse the List: Write a function to go through each node in the list, starting from the first one. While traversing each node, it will print out the data stored in that node. This function will keep moving to the next node until it reaches the end of the list.
Below is the implementation of the above approach:
C++
// C++ program to implement Recursive insertion and
// traversal in a linked list
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int new_data) {
data = new_data;
next = nullptr;
}
};
// 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
if (head == nullptr)
return new Node(data);
// If we have not reached the end,
// keep traversing recursively
head->next = insertEnd(head->next, data);
return head;
}
// Function to traverse and print the linked list
// starting from the head node, recursively
void traverse(Node* head) {
if (head == nullptr)
return;
cout << head->data << " ";
// Recur for the remaining list
traverse(head->next);
}
int main() {
// Insert nodes with data 1, 2, 3, 4, 5
Node* head = nullptr;
head = insertEnd(head, 1);
head = insertEnd(head, 2);
head = insertEnd(head, 3);
head = insertEnd(head, 4);
head = insertEnd(head, 5);
traverse(head);
}
C
// C program to implement Recursive insertion and
// traversal in a linked list
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* newNode(int new_data);
// Function to insert a new node at the
// end of linked list using recursion.
struct Node* insertEnd(struct Node* head, int data) {
// If linked list is empty, create a new node
if (head == NULL)
return newNode(data);
// If we have not reached the end,
// keep traversing recursively
head->next = insertEnd(head->next, data);
return head;
}
// Function to traverse and print the linked list
// starting from the head node, recursively
void traverse(struct Node* head) {
if (head == NULL) return;
printf("%d ", head->data);
// Recur for the remaining list
traverse(head->next);
}
struct Node* newNode(int new_data) {
struct Node* new_node =
(struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = NULL;
return new_node;
}
int main() {
// Insert nodes with data 1, 2, 3, 4, 5
struct Node* head = NULL;
head = insertEnd(head, 1);
head = insertEnd(head, 2);
head = insertEnd(head, 3);
head = insertEnd(head, 4);
head = insertEnd(head, 5);
traverse(head);
return 0;
}
Java
// Java program to implement Recursive insertion and
// traversal in a linked list
class Node {
int data;
Node next;
Node(int new_data) {
data = new_data;
next = null;
}
}
public class GfG {
// 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
if (head == null)
return new Node(data);
// If we have not reached the end
// keep traversing recursively
head.next = insertEnd(head.next, data);
return head;
}
// Function to traverse and print the linked list
// starting from the head node, recursively
static void traverse(Node head) {
if (head == null) return;
System.out.print(head.data + " ");
// Recur for the remaining list
traverse(head.next);
}
public static void main(String[] args) {
// Insert nodes with data 1, 2, 3, 4, 5
Node head = null;
head = insertEnd(head, 1);
head = insertEnd(head, 2);
head = insertEnd(head, 3);
head = insertEnd(head, 4);
head = insertEnd(head, 5);
traverse(head);
}
}
Python
# Python program to implement Recursive insertion and
# traversal in a linked list
class Node:
def __init__(self, new_data):
self.data = new_data
self.next = None
# Function to insert a new node at the
# end of linked list using recursion.
def insert_end(head, data):
# If linked list is empty, create a new node
if head is None:
return Node(data)
# If we have not reached the end,
# keep traversing recursively
head.next = insert_end(head.next, data)
return head
# Function to traverse and print the linked list
# starting from the head node, recursively
def traverse(head):
if head is None:
return
print(head.data, end=" ")
# Recur for the remaining list
traverse(head.next)
if __name__ == "__main__":
# Insert nodes with data 1, 2, 3, 4, 5
head = None
head = insert_end(head, 1)
head = insert_end(head, 2)
head = insert_end(head, 3)
head = insert_end(head, 4)
head = insert_end(head, 5)
traverse(head)
C#
// C# program to implement Recursive insertion and
// traversal in a linked list
using System;
class Node {
public int data;
public Node next;
public Node(int new_data) {
data = new_data;
next = null;
}
}
class GfG {
// 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
if (head == null)
return new Node(data);
// If we have not reached the end,
// keep traversing recursively
head.next = InsertEnd(head.next, data);
return head;
}
// Function to traverse and print the linked list
// starting from the head node, recursively
static void Traverse(Node head) {
if (head == null)
return;
Console.Write(head.data + " ");
// Recur for the remaining list
Traverse(head.next);
}
static void Main(string[] args) {
// Insert nodes with data 1, 2, 3, 4, 5
Node head = null;
head = InsertEnd(head, 1);
head = InsertEnd(head, 2);
head = InsertEnd(head, 3);
head = InsertEnd(head, 4);
head = InsertEnd(head, 5);
Traverse(head);
}
}
JavaScript
// Javascript program to implement Recursive insertion and
// traversal in a linked list
class Node {
constructor(new_data) {
this.data = new_data;
this.next = null;
}
}
// 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
if (head === null)
return new Node(data);
// If we have not reached the end,
// keep traversing recursively
head.next = insertEnd(head.next, data);
return head;
}
// Function to traverse and print the linked list
// starting from the head node, recursively
function traverse(head) {
if (head === null)
return;
console.log(head.data + " ");
// Recur for the remaining list
traverse(head.next);
}
// Insert nodes with data 1, 2, 3, 4, 5
let head = null;
head = insertEnd(head, 1);
head = insertEnd(head, 2);
head = insertEnd(head, 3);
head = insertEnd(head, 4);
head = insertEnd(head, 5);
traverse(head);
Time Complexity: O(n), to traverse the linked list of size n
Auxiliary Space: O(n), for recursion call stack
Similar Reads
Insertion in Unrolled Linked List
An unrolled linked list is a linked list of small arrays, all of the same size where each is so small that the insertion or deletion is fast and quick, but large enough to fill the cache line. An iterator pointing into the list consists of both a pointer to a node and an index into that node contain
11 min read
Find Length of a Linked List (Iterative and Recursive)
Given a Singly Linked List, the task is to find the Length of the Linked List. Examples: Input: LinkedList = 1->3->1->2->1Output: 5Explanation: The linked list has 5 nodes. Input: LinkedList = 2->4->1->9->5->3->6Output: 7 Explanation: The linked list has 7 nodes. Input:
11 min read
Search an element in a Linked List (Iterative and Recursive)
Given a linked list and a key, the task is to check if key is present in the linked list or not. Examples: Input: 14 -> 21 -> 11 -> 30 -> 10, key = 14Output: YesExplanation: 14 is present in the linked list. Input: 6 -> 21 -> 17 -> 30 -> 10 -> 8, key = 13Output: NoExplanat
13 min read
Traversal in Doubly Linked List
Traversal of Doubly Linked List is one of the fundamental operations, where we traverse or visit each node of the linked list. In this article, we will cover how to traverse all the nodes of a doubly linked list and its implementation. Examples: Input: 10 <-> 20 <-> 30 <-> 40Output
15+ min read
Insertion in Linked List
Insertion in a linked list involves adding a new node at a specified position in the list. There are several types of insertion based on the position where the new node is to be added: At the front of the linked list Before a given node.After a given node.At a specific position.At the end of the lin
4 min read
Insertion in Circular Singly Linked List
In this article, we will learn how to insert a node into a circular linked list. Insertion is a fundamental operation in linked lists that involves adding a new node to the list. In a circular linked list, the last node connects back to the first node, creating a loop. There are four main ways to ad
12 min read
Reverse a linked list using recursion
Given a linked list, the task is to reverse the linked list by changing the links between nodes. Examples: Input: Linked List = 1 -> 2 -> 3 -> 4 -> NULL Output: 4 -> 3 -> 2 -> 1 -> NULL Input: Linked List = 1 -> 2 -> 3 -> 4 -> 5 -> NULL Output: 5 -> 4 ->
6 min read
Insertion Sort for Singly Linked List
Given a singly linked list, the task is to sort the list (in ascending order) using the insertion sort algorithm. Examples: Input: 5->4->1->3->2Output: 1->2->3->4->5Input: 4->3->2->1Output: 1->2->3->4 The prerequisite is Insertion Sort on Array. The idea is
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->prev and head->next If prev = NULL it means th
9 min read
Insert Node at the End of a Linked List
Given a linked list, the task is to insert a new node at the end of the linked list. Examples: Input: LinkedList = 2 -> 3 -> 4 -> 5, NewNode = 1Output: LinkedList = 2 -> 3 -> 4 -> 5 -> 1 Input: LinkedList = NULL, NewNode = 1Output: LinkedList = 1 Approach:Â Inserting at the end
9 min read