Insert a node after the n-th node from the end
Insert a node x after the nth node from the end in the given singly linked list. It is guaranteed that the list contains the nth node from the end. Also 1 <= n.
Examples:
Input : list: 1->3->4->5
n = 4, x = 2
Output : 1->2->3->4->5
4th node from the end is 1 and
insertion has been done after this node.
Input : list: 10->8->3->12->5->18
n = 2, x = 11
Output : 10->8->3->12->5->11->18
Method 1 (Using length of the list): Find the length of the linked list, i.e, the number of nodes in the list. Let it be len. Now traverse the list from the 1st node upto the (len-n+1)th node from the beginning and insert the new node after this node. This method requires two traversals of the list.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode( int data)
{
Node* newNode = (Node*) malloc ( sizeof (Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAfterNthNode(Node* head, int n, int x)
{
if (head == NULL)
return ;
Node* newNode = getNode(x);
Node* ptr = head;
int len = 0, i;
while (ptr != NULL) {
len++;
ptr = ptr->next;
}
ptr = head;
for (i = 1; i <= (len - n); i++)
ptr = ptr->next;
newNode->next = ptr->next;
ptr->next = newNode;
}
void printList(Node* head)
{
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
}
int main()
{
Node* head = getNode(1);
head->next = getNode(3);
head->next->next = getNode(4);
head->next->next->next = getNode(5);
int n = 4, x = 2;
cout << "Original Linked List: " ;
printList(head);
insertAfterNthNode(head, n, x);
cout << "\nLinked List After Insertion: " ;
printList(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* getNode( int data)
{
Node* newNode = (Node*) malloc ( sizeof (Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAfterNthNode(Node* head, int n, int x)
{
if (head == NULL)
return ;
Node* newNode = getNode(x);
Node* ptr = head;
int len = 0, i;
while (ptr != NULL) {
len++;
ptr = ptr->next;
}
ptr = head;
for (i = 1; i <= (len - n); i++)
ptr = ptr->next;
newNode->next = ptr->next;
ptr->next = newNode;
}
void printList(Node* head)
{
while (head != NULL) {
printf ( "%d " , head->data);
head = head->next;
}
}
int main()
{
Node* head = getNode(1);
head->next = getNode(3);
head->next->next = getNode(4);
head->next->next->next = getNode(5);
int n = 4, x = 2;
printf ( "Original Linked List: " );
printList(head);
insertAfterNthNode(head, n, x);
printf ( "\nLinked List After Insertion: " );
printList(head);
return 0;
}
|
Java
class GfG
{
static class Node
{
int data;
Node next;
}
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static void insertAfterNthNode(Node head, int n, int x)
{
if (head == null )
return ;
Node newNode = getNode(x);
Node ptr = head;
int len = 0 , i;
while (ptr != null )
{
len++;
ptr = ptr.next;
}
ptr = head;
for (i = 1 ; i <= (len - n); i++)
ptr = ptr.next;
newNode.next = ptr.next;
ptr.next = newNode;
}
static void printList(Node head)
{
while (head != null )
{
System.out.print(head.data + " " );
head = head.next;
}
}
public static void main(String[] args)
{
Node head = getNode( 1 );
head.next = getNode( 3 );
head.next.next = getNode( 4 );
head.next.next.next = getNode( 5 );
int n = 4 , x = 2 ;
System.out.print( "Original Linked List: " );
printList(head);
insertAfterNthNode(head, n, x);
System.out.println();
System.out.print( "Linked List After Insertion: " );
printList(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def getNode(data) :
newNode = Node( 0 )
newNode.data = data
newNode. next = None
return newNode
def insertAfterNthNode(head, n, x) :
if (head = = None ) :
return
newNode = getNode(x)
ptr = head
len = 0
i = 0
while (ptr ! = None ) :
len = len + 1
ptr = ptr. next
ptr = head
i = 1
while ( i < = ( len - n) ) :
ptr = ptr. next
i = i + 1
newNode. next = ptr. next
ptr. next = newNode
def printList( head) :
while (head ! = None ):
print (head.data ,end = " " )
head = head. next
head = getNode( 1 )
head. next = getNode( 3 )
head. next . next = getNode( 4 )
head. next . next . next = getNode( 5 )
n = 4
x = 2
print ( "Original Linked List: " )
printList(head)
insertAfterNthNode(head, n, x)
print ()
print ( "Linked List After Insertion: " )
printList(head)
|
C#
using System;
class GfG
{
public class Node
{
public int data;
public Node next;
}
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static void insertAfterNthNode(Node head, int n, int x)
{
if (head == null )
return ;
Node newNode = getNode(x);
Node ptr = head;
int len = 0, i;
while (ptr != null )
{
len++;
ptr = ptr.next;
}
ptr = head;
for (i = 1; i <= (len - n); i++)
ptr = ptr.next;
newNode.next = ptr.next;
ptr.next = newNode;
}
static void printList(Node head)
{
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
}
public static void Main(String[] args)
{
Node head = getNode(1);
head.next = getNode(3);
head.next.next = getNode(4);
head.next.next.next = getNode(5);
int n = 4, x = 2;
Console.Write( "Original Linked List: " );
printList(head);
insertAfterNthNode(head, n, x);
Console.WriteLine();
Console.Write( "Linked List After Insertion: " );
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function getNode(data) {
var newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
function insertAfterNthNode(head , n , x) {
if (head == null )
return ;
var newNode = getNode(x);
var ptr = head;
var len = 0, i;
while (ptr != null ) {
len++;
ptr = ptr.next;
}
ptr = head;
for (i = 1; i <= (len - n); i++)
ptr = ptr.next;
newNode.next = ptr.next;
ptr.next = newNode;
}
function printList(head) {
while (head != null ) {
document.write(head.data + " " );
head = head.next;
}
}
var head = getNode(1);
head.next = getNode(3);
head.next.next = getNode(4);
head.next.next.next = getNode(5);
var n = 4, x = 2;
document.write( "Original Linked List: " );
printList(head);
insertAfterNthNode(head, n, x);
document.write();
document.write( "<br/>Linked List After Insertion: " );
printList(head);
</script>
|
Output
Original Linked List: 1 3 4 5
Linked List After Insertion: 1 2 3 4 5
Time Complexity: O(n), where n is the number of nodes in the list.
Auxiliary Space: O(1)
Method 2 (Single traversal): This method uses two pointers, one is slow_ptr and the other is fast_ptr. First move the fast_ptr up to the nth node from the beginning. Make the slow_ptr point to the 1st node of the list. Now, simultaneously move both the pointers until fast_ptr points to the last node. At this point the slow_ptr will be pointing to the nth node from the end. Insert the new node after this node. This method requires single traversal of the list.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode( int data)
{
Node* newNode = (Node*) malloc ( sizeof (Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAfterNthNode(Node* head, int n, int x)
{
if (head == NULL)
return ;
Node* newNode = getNode(x);
Node* slow_ptr = head;
Node* fast_ptr = head;
for ( int i = 1; i <= n - 1; i++)
fast_ptr = fast_ptr->next;
while (fast_ptr->next != NULL) {
slow_ptr = slow_ptr->next;
fast_ptr = fast_ptr->next;
}
newNode->next = slow_ptr->next;
slow_ptr->next = newNode;
}
void printList(Node* head)
{
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
}
int main()
{
Node* head = getNode(1);
head->next = getNode(3);
head->next->next = getNode(4);
head->next->next->next = getNode(5);
int n = 4, x = 2;
cout << "Original Linked List: " ;
printList(head);
insertAfterNthNode(head, n, x);
cout << "\nLinked List After Insertion: " ;
printList(head);
return 0;
}
|
Java
class GfG
{
static class Node
{
int data;
Node next;
}
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static void insertAfterNthNode(Node head,
int n, int x)
{
if (head == null )
return ;
Node newNode = getNode(x);
Node slow_ptr = head;
Node fast_ptr = head;
for ( int i = 1 ; i <= n - 1 ; i++)
fast_ptr = fast_ptr.next;
while (fast_ptr.next != null )
{
slow_ptr = slow_ptr.next;
fast_ptr = fast_ptr.next;
}
newNode.next = slow_ptr.next;
slow_ptr.next = newNode;
}
static void printList(Node head)
{
while (head != null )
{
System.out.print(head.data + " " );
head = head.next;
}
}
public static void main(String[] args)
{
Node head = getNode( 1 );
head.next = getNode( 3 );
head.next.next = getNode( 4 );
head.next.next.next = getNode( 5 );
int n = 4 , x = 2 ;
System.out.println( "Original Linked List: " );
printList(head);
insertAfterNthNode(head, n, x);
System.out.println();
System.out.println( "Linked List After Insertion: " );
printList(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def getNode(data):
newNode = Node(data)
return newNode
def insertAfterNthNode(head, n, x):
if (head = = None ):
return
newNode = getNode(x)
slow_ptr = head
fast_ptr = head
for i in range ( 1 , n):
fast_ptr = fast_ptr. next
while (fast_ptr. next ! = None ):
slow_ptr = slow_ptr. next
fast_ptr = fast_ptr. next
newNode. next = slow_ptr. next
slow_ptr. next = newNode
def printList(head):
while (head ! = None ):
print (head.data, end = ' ' )
head = head. next
if __name__ = = '__main__' :
head = getNode( 1 )
head. next = getNode( 3 )
head. next . next = getNode( 4 )
head. next . next . next = getNode( 5 )
n = 4
x = 2
print ( "Original Linked List: " , end = '')
printList(head)
insertAfterNthNode(head, n, x)
print ( "\nLinked List After Insertion: " , end = '')
printList(head)
|
C#
using System;
class GfG
{
public class Node
{
public int data;
public Node next;
}
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static void insertAfterNthNode(Node head,
int n, int x)
{
if (head == null )
return ;
Node newNode = getNode(x);
Node slow_ptr = head;
Node fast_ptr = head;
for ( int i = 1; i <= n - 1; i++)
fast_ptr = fast_ptr.next;
while (fast_ptr.next != null )
{
slow_ptr = slow_ptr.next;
fast_ptr = fast_ptr.next;
}
newNode.next = slow_ptr.next;
slow_ptr.next = newNode;
}
static void printList(Node head)
{
while (head != null )
{
Console.Write(head.data + " " );
head = head.next;
}
}
public static void Main()
{
Node head = getNode(1);
head.next = getNode(3);
head.next.next = getNode(4);
head.next.next.next = getNode(5);
int n = 4, x = 2;
Console.WriteLine( "Original Linked List: " );
printList(head);
insertAfterNthNode(head, n, x);
Console.WriteLine();
Console.WriteLine( "Linked List After Insertion: " );
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function getNode(data) {
var newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
function insertAfterNthNode(head, n, x) {
if (head == null ) return ;
var newNode = getNode(x);
var slow_ptr = head;
var fast_ptr = head;
for ( var i = 1; i <= n - 1; i++)
fast_ptr = fast_ptr.next;
while (fast_ptr.next != null ) {
slow_ptr = slow_ptr.next;
fast_ptr = fast_ptr.next;
}
newNode.next = slow_ptr.next;
slow_ptr.next = newNode;
}
function printList(head) {
while (head != null ) {
document.write(head.data + " " );
head = head.next;
}
}
var head = getNode(1);
head.next = getNode(3);
head.next.next = getNode(4);
head.next.next.next = getNode(5);
var n = 4,
x = 2;
document.write( "Original Linked List: " );
printList(head);
insertAfterNthNode(head, n, x);
document.write( "<br>" );
document.write( "Linked List After Insertion:" );
printList(head);
</script>
|
Output
Original Linked List: 1 3 4 5
Linked List After Insertion: 1 2 3 4 5
Time Complexity: O(n), where n is the number of nodes in the list.
Auxiliary Space: O(1)
Method 3 (Recursive Approach):
- Traverse the list recursively till we reach the last node.
- while back tracking insert the node at desired position.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode( int data)
{
Node* newNode = new Node();
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void insertAfterNthNode(Node* head, int x, int & n)
{
if (head == NULL)
return ;
insertAfterNthNode(head->next, x, n);
if (--n == 0) {
Node* temp = getNode(x);
temp->next = head->next;
head->next = temp;
}
}
void printList(Node* head)
{
while (head != NULL) {
cout << head->data << " " ;
head = head->next;
}
}
int main()
{
Node* head = getNode(1);
head->next = getNode(3);
head->next->next = getNode(4);
head->next->next->next = getNode(5);
int n = 4, x = 2;
cout << "Original Linked List: " ;
printList(head);
insertAfterNthNode(head, x, n);
cout << "\nLinked List After Insertion: " ;
printList(head);
return 0;
}
|
Java
class GfG {
static class Node {
int data;
Node next;
}
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static int n;
static void insertAfterNthNode(Node head, int x)
{
if (head == null )
return ;
insertAfterNthNode(head.next, x);
if (--n == 0 ) {
Node temp = getNode(x);
temp.next = head.next;
head.next = temp;
}
}
static void printList(Node head)
{
while (head != null ) {
System.out.print(head.data + " " );
head = head.next;
}
}
public static void main(String[] args)
{
Node head = getNode( 1 );
head.next = getNode( 3 );
head.next.next = getNode( 4 );
head.next.next.next = getNode( 5 );
n = 4 ;
int x = 2 ;
System.out.println( "Original Linked List: " );
printList(head);
insertAfterNthNode(head, x);
System.out.println();
System.out.println( "Linked List After Insertion: " );
printList(head);
}
}
|
Python3
class Node:
def __init__( self ,data):
self .data = data
self . next = None
def getNode(data):
newNode = Node(data)
return newNode
def insertAfterNthNode(head,x,n):
if head = = None :
return n - 1
n = insertAfterNthNode(head. next ,x,n)
if n = = 0 :
temp = getNode(x)
temp. next = head. next
head. next = temp
return n - 1
def printList(head):
while head ! = None :
print (head.data,end = ' ' )
head = head. next
print ()
head = getNode( 1 )
head. next = getNode( 3 )
head. next . next = getNode( 4 )
head. next . next . next = getNode( 5 )
n = 4
x = 2
print ( "Original Linked List: " )
printList(head)
insertAfterNthNode(head, x, n)
print ( "Linked List After Insertion: " )
printList(head)
|
C#
using System;
public class GFG {
class Node {
public int data;
public Node next;
}
static Node getNode( int data)
{
Node newNode = new Node();
newNode.data = data;
newNode.next = null ;
return newNode;
}
static int n;
static void insertAfterNthNode(Node head, int x)
{
if (head == null )
return ;
insertAfterNthNode(head.next, x);
if (--n == 0) {
Node temp = getNode(x);
temp.next = head.next;
head.next = temp;
}
}
static void printList(Node head)
{
while (head != null ) {
Console.Write(head.data + " " );
head = head.next;
}
}
static public void Main()
{
Node head = getNode(1);
head.next = getNode(3);
head.next.next = getNode(4);
head.next.next.next = getNode(5);
n = 4;
int x = 2;
Console.Write( "Original Linked List: " );
printList(head);
insertAfterNthNode(head, x);
Console.WriteLine();
Console.Write( "Linked List After Insertion: " );
printList(head);
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .next = null ;
}
}
let n;
function insertAfterNthNode(head, x) {
if (head == null ) return ;
insertAfterNthNode(head.next, x);
if (--n == 0) {
let temp = new Node(x);
temp.next = head.next;
head.next = temp;
}
}
function printList(head) {
let current = head;
while (current != null ) {
console.log(current.data + " " );
current = current.next;
}
console.log( "<br>" );
}
let head = new Node(1);
head.next = new Node(3);
head.next.next = new Node(4);
head.next.next.next = new Node(5);
n = 4;
let x = 2;
console.log( "Original Linked List: " );
printList(head);
insertAfterNthNode(head, x);
console.log( "\nLinked List After Insertion: " );
printList(head);
|
Output
Original Linked List: 1 3 4 5
Linked List After Insertion: 1 2 3 4 5
Time Complexity: O(n)
Where n is the number of nodes in the list.
Auxiliary Space: O(n)
Due to recursion call stack.
Last Updated :
02 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...