# 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.

```// C++ implementation to insert a node after
// the n-th node from the end
#include <bits/stdc++.h>
using namespace std;

// structure of a node
struct Node {
int data;
Node* next;
};

// function to get a new node
Node* getNode(int data)
{
// allocate memory for the node
Node* newNode = (Node*)malloc(sizeof(Node));

// put in the data
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// function to insert a node after the
// nth node from the end
void insertAfterNthNode(Node* head, int n, int x)
{
// if list is empty
return;

// get a new node for the value 'x'
Node* newNode = getNode(x);
int len = 0, i;

// find length of the list, i.e, the
// number of nodes in the list
while (ptr != NULL) {
len++;
ptr = ptr->next;
}

// traverse up to the nth node from the end
for (i = 1; i <= (len - n); i++)
ptr = ptr->next;

// insert the 'newNode' by making the
newNode->next = ptr->next;
ptr->next = newNode;
}

// function to print the list
{
cout << head->data << " ";
}
}

// Driver program to test above
int main()
{
// Creating list 1->3->4->5

int n = 4, x = 2;

cout << "Original Linked List: ";

cout << "\nLinked List After Insertion: ";

return 0;
}
```

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.

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.

```// C++ implementation to insert a node after the
// nth node from the end
#include <bits/stdc++.h>

using namespace std;

// structure of a node
struct Node {
int data;
Node* next;
};

// function to get a new node
Node* getNode(int data)
{
// allocate memory for the node
Node* newNode = (Node*)malloc(sizeof(Node));

// put in the data
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// function to insert a node after the
// nth node from the end
void insertAfterNthNode(Node* head, int n, int x)
{
// if list is empty
return;

// get a new node for the value 'x'
Node* newNode = getNode(x);

// Initializing the slow and fast pointers

// move 'fast_ptr' to point to the nth node
// from the beginning
for (int i = 1; i <= n - 1; i++)
fast_ptr = fast_ptr->next;

// iterate until 'fast_ptr' points to the
// last node
while (fast_ptr->next != NULL) {

// move both the pointers to the
// respective next nodes
slow_ptr = slow_ptr->next;
fast_ptr = fast_ptr->next;
}

// insert the 'newNode' by making the
newNode->next = slow_ptr->next;
slow_ptr->next = newNode;
}

// function to print the list
{
cout << head->data << " ";
}
}

// Driver program to test above
int main()
{
// Creating list 1->3->4->5

int n = 4, x = 2;

cout << "Original Linked List: ";

cout << "\nLinked List After Insertion: ";

return 0;
}
```

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.

This article is contributed by Ayush Jauhari. 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.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1.6 Average Difficulty : 1.6/5.0
Based on 3 vote(s)