Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to insert a Node in a Singly Linked List at a given Position using Recursion

  • Last Updated : 10 Dec, 2021

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->2->3->4->5->6->7, node = (val=100,next=null), position = 4 
Output: 1->2->3->100->4->5->6->7
Explanation: Here the node with value 100 is being inserted at the 4th position. Initially at the 4th position the value is 4. Now when 100 is inserted at 4th position all the value starting from 4 will shift 1 position to its right. This can be visualised in the following way:

1->2->3->4->5->6->7
1->2->3->100->4->5->6->7

Input: list = 1->2->3->100->4->5->6->7, node = (val=101,next=null), position = 1  
Output: 10->1->2->3->100->4->5->6->7

 

Solution:

There will be 3 cases:

  1. Insert a node at first position of linked list
    • Approach: Follow the steps mentioned below:
      1. Create a temporary node.
      2. Now insert the temporary node before head and change the head pointer to point the temporary node.
  2. Insert a node in between first and last node of linked list
    • Approach: Follow the steps mentioned below:
      1. Call the recursive function to reach to the desired position.
      2. If the position is greater than the length of the list then insertion is not possible.
      3. If not, then insert the new node at the desired position.
  3. Insert a node at the end of linked list
    • Approach: Follow the steps mentioned below:
      1. Recursively move to the end of the linked list.
      2. Insert the new node at the end of the list.

Recurrence relation: T(n) = T(n-1) + c

Below is the implementation of above approach

C++




// C++ code to insert a node
// at a given position
// in singly linked list
#include <bits/stdc++.h>
#define null nullptr
 
using namespace std;
 
// Structure of the node of linked list
struct node {
    int item;
    node* nxt;
    node(int item, node* t)
    {
        this->item = item;
        (*this).nxt = t;
    }
};
 
// Function to insert a node
// at the first position
node* insertAtFirst(node*& listHead, node* x)
{
    node* temp = listHead;
    x->nxt = temp;
    listHead = temp = x;
    return temp;
}
 
// Function to insert a node
// at the last position
node* insertAtEnd(node* listHead, node* x)
{
    if (listHead->nxt == null) {
        listHead->nxt = x;
        return listHead;
    }
    listHead->nxt
      = insertAtEnd(listHead->nxt, x);
    return listHead;
}
 
// Function to insert a node
// in between first and last
node* insertInBetween(node* temp,
                      node* x, int pos)
{
    if (pos == 2) {
        if (temp != null) {
            x->nxt = temp->nxt;
            temp->nxt = x;
        }
        return temp;
    }
    if (temp == null) {
        return temp;
    }
    temp->nxt
      = insertInBetween(temp->nxt, x, --pos);
}
 
// Printing through recursion
void print(node* head)
{
    if (head == null) {
        return;
    }
    cout << head->item << " ";
    print(head->nxt);
}
 
// Driver code
int main()
{
    node* head = new node(1, 0);
 
    // Creating a linked list of length 15
    for (int i = 2; i < 16; i++)
        insertAtEnd(head, new node(i, 0));
 
    // Insert node with value 100
    // at 4th position
    insertInBetween(head,
                    new node(100, 0), 4);
 
    // Insert node with value 101
    // at 200th position
    insertInBetween(head,
                    new node(101, 0), 200);
 
    // Insert node with value 100
    // at 1st position
    insertAtFirst(head, new node(100, 0));
 
    // Insert node with value 100
    // at the end position
    insertAtEnd(head, new node(100, 0));
 
    // Printing the new linked list
    print(head);
    return 0;
}

Java




// Java code to insert a node at a given
//position in singly linked list
class GFG{
 
// Structure of the node of linked list
static class node
{
    int item;
    node nxt;
     
    node(int item, node t)
    {
        this.item = item;
        this.nxt = t;
    }
};
 
// Function to insert a node
// at the first position
static node insertAtFirst(node listHead, node x)
{
    x.nxt = listHead;
    listHead = null;
    listHead = x;
    return listHead;
}
 
// Function to insert a node
// at the last position
static node insertAtEnd(node listHead, node x)
{
    if (listHead.nxt == null)
    {
        listHead.nxt = x;
        return listHead;
    }
    listHead.nxt = insertAtEnd(listHead.nxt, x);
    return listHead;
}
 
// Function to insert a node
// in between first and last
static node insertInBetween(node temp, node x,
                            int pos)
{
    if (pos == 2)
    {
        if (temp != null)
        {
            x.nxt = temp.nxt;
            temp.nxt = x;
        }
        return temp;
    }
    if (temp == null)
    {
        return temp;
    }
    temp.nxt = insertInBetween(temp.nxt, x, --pos);
    return temp;
}
 
// Printing through recursion
static void print(node head)
{
    if (head == null)
    {
        return;
    }
    System.out.print(head.item + " ");
    print(head.nxt);
}
 
// Driver code
public static void main(String[] args)
{
    node head = new node(1, null);
 
    // Creating a linked list of length 15
    for(int i = 2; i < 16; i++)
        insertAtEnd(head, new node(i, null));
 
    // Insert node with value 100
    // at 4th position
    head = insertInBetween(head,
                    new node(100, null), 4);
 
    // Insert node with value 101
    // at 200th position
    head = insertInBetween(head,
                    new node(101, null), 200);
 
    // Insert node with value 100
    // at 1st position
    head = insertAtFirst(head, new node(100, null));
 
    // Insert node with value 100
    // at the end position
    head = insertAtEnd(head, new node(100, null));
 
    // Printing the new linked list
    print(head);
}
}
 
// This code is contributed by shikhasingrajput

C#




// C# code to insert a node at a given
//position in singly linked list
using System;
 
public class GFG{
 
// Structure of the node of linked list
class node
{
    public int item;
    public node nxt;
     
    public node(int item, node t)
    {
        this.item = item;
        this.nxt = t;
    }
};
 
// Function to insert a node
// at the first position
static node insertAtFirst(node listHead, node x)
{
    x.nxt = listHead;
    listHead = null;
    listHead = x;
    return listHead;
}
 
// Function to insert a node
// at the last position
static node insertAtEnd(node listHead, node x)
{
    if (listHead.nxt == null)
    {
        listHead.nxt = x;
        return listHead;
    }
    listHead.nxt = insertAtEnd(listHead.nxt, x);
    return listHead;
}
 
// Function to insert a node
// in between first and last
static node insertInBetween(node temp, node x,
                            int pos)
{
    if (pos == 2)
    {
        if (temp != null)
        {
            x.nxt = temp.nxt;
            temp.nxt = x;
        }
        return temp;
    }
    if (temp == null)
    {
        return temp;
    }
    temp.nxt = insertInBetween(temp.nxt, x, --pos);
    return temp;
}
 
// Printing through recursion
static void print(node head)
{
    if (head == null)
    {
        return;
    }
    Console.Write(head.item + " ");
    print(head.nxt);
}
 
// Driver code
public static void Main(String[] args)
{
    node head = new node(1, null);
 
    // Creating a linked list of length 15
    for(int i = 2; i < 16; i++)
        insertAtEnd(head, new node(i, null));
 
    // Insert node with value 100
    // at 4th position
    head = insertInBetween(head,
                    new node(100, null), 4);
 
    // Insert node with value 101
    // at 200th position
    head = insertInBetween(head,
                    new node(101, null), 200);
 
    // Insert node with value 100
    // at 1st position
    head = insertAtFirst(head, new node(100, null));
 
    // Insert node with value 100
    // at the end position
    head = insertAtEnd(head, new node(100, null));
 
    // Printing the new linked list
    print(head);
}
}
 
// This code is contributed by shikhasingrajput

Javascript




<script>
// Javascript code to insert a node at a given
// position in singly linked list
 
// Structure of the node of linked list
class node {
 
    constructor(item, t) {
        this.item = item;
        this.nxt = t;
    }
};
 
// Function to insert a node
// at the first position
function insertAtFirst(listHead, x) {
    x.nxt = listHead;
    listHead = null;
    listHead = x;
    return listHead;
}
 
// Function to insert a node
// at the last position
function insertAtEnd(listHead, x) {
    if (listHead.nxt == null) {
        listHead.nxt = x;
        return listHead;
    }
    listHead.nxt = insertAtEnd(listHead.nxt, x);
    return listHead;
}
 
// Function to insert a node
// in between first and last
function insertInBetween(temp, x, pos) {
    if (pos == 2) {
        if (temp != null) {
            x.nxt = temp.nxt;
            temp.nxt = x;
        }
        return temp;
    }
    if (temp == null) {
        return temp;
    }
    temp.nxt = insertInBetween(temp.nxt, x, --pos);
    return temp;
}
 
// Printing through recursion
function _print(head) {
    if (head == null) {
        return;
    }
    document.write(head.item + " ");
    _print(head.nxt);
}
 
// Driver code
let head = new node(1, null);
 
// Creating a linked list of length 15
for (let i = 2; i < 16; i++)
    insertAtEnd(head, new node(i, null));
 
// Insert node with value 100
// at 4th position
head = insertInBetween(head,
    new node(100, null), 4);
 
// Insert node with value 101
// at 200th position
head = insertInBetween(head,
    new node(101, null), 200);
 
// Insert node with value 100
// at 1st position
head = insertAtFirst(head, new node(100, null));
 
// Insert node with value 100
// at the end position
head = insertAtEnd(head, new node(100, null));
 
// Printing the new linked list
_print(head);
 
    // This code is contributed by Saurabh Jaiswal
</script>
Output
100 1 2 3 100 4 5 6 7 8 9 10 11 12 13 14 15 100 

Time Complexity: O(N) where N is the size of linked list
Auxiliary Space: O(N) where N is the size of linked list

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!