Insert a node at a specific position in a linked list (original) (raw)
Last Updated : 21 Oct, 2025
Given a **head of singly linked list, a position **pos, and **val, Insert that data into a linked list at the given position.
**Examples:
**Input: val = 3, pos = 3
**Output: 1 -> 2 -> 3 -> 4
**Explanation: Node inserted at position 3.
[Approach] Using Iterative Method - O(n) time and O(1) space:
The idea is simple: create a new node, then find the spot where it should be placed. Walk through the list until you reach the node just before that position. Link the new node’s next to the following node, and adjust the previous node’s next to point to the new node.
**Step By Step Implementations:
- Initialize a variable , say curr points to head and allocate the memory to the new node with the given val.
- Traverse the Linked list using curr pointer upto position-1 nodes.
- If curr's next is not null , then next pointer of the new node points to the next of curr node.
- The next pointer of current node points to the new node.
- return the head of linked list. C++ `
#include using namespace std;
class Node { public: int val; Node *next; Node(int x) { val = x; next = nullptr; } };
Node *insertPos(Node *head, int pos, int val) {
if (pos < 1)
return head;
// head will change if pos=1
if (pos == 1) {
Node *newNode = new Node(val);
newNode->next = head;
return newNode;
}
Node *curr = head;
// Traverse to the node that will be
// present just before the new node
for (int i = 1; i < pos - 1 && curr != nullptr; i++) {
curr = curr->next;
}
// If position is greater than the
// number of nodes
if (curr == nullptr)
return head;
Node *newNode = new Node(val);
// update the next pointers
newNode->next = curr->next;
curr->next = newNode;
return head;}
void printList(Node *head) {
Node *curr = head;
while (curr != nullptr) {
cout << curr->val;
if (curr->next != nullptr) {
cout << " -> ";
}
curr = curr->next;
}
cout << endl;
}
int main() {
// Creating the list 1->2->4
Node *head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(4);
int val = 3, pos = 3;
head = insertPos(head, pos, val);
printList(head);
return 0;}
C
#include <stdio.h> #include <stdlib.h>
// Define a linked list node struct Node { int val; struct Node *next; };
// Function to create a new node struct Node *createNode(int x) { struct Node *newNode = (struct Node *)malloc(sizeof(struct Node)); newNode->val = x; newNode->next = NULL; return newNode; }
// Function to insert a new node at a specific position struct Node *insertPos(struct Node *head, int pos, int val) {
if (pos < 1)
return head;
// If inserting at the head
if (pos == 1)
{
struct Node *newNode = createNode(val);
newNode->next = head;
return newNode;
}
struct Node *curr = head;
// Traverse to the node just before the new position
for (int i = 1; i < pos - 1 && curr != NULL; i++)
{
curr = curr->next;
}
// If position is greater than the number of nodes
if (curr == NULL)
return head;
// Create and insert the new node
struct Node *newNode = createNode(val);
newNode->next = curr->next;
curr->next = newNode;
return head;}
// Function to print the linked list void printList(struct Node *head) { struct Node *curr = head; while (curr != NULL) { printf("%d", curr->val); if (curr->next != NULL) printf(" -> "); curr = curr->next; } printf("\n"); }
int main() { // Creating the list 1->2->4 struct Node *head = createNode(1); head->next = createNode(2); head->next->next = createNode(4);
int val = 3, pos = 3;
// Insert at the given position
head = insertPos(head, pos, val);
// Print the final list
printList(head);
return 0;}
Java
class Node { int val; Node next;
Node(int x) {
val = x;
next = null;
}}
class GfG { static Node insertPos(Node head, int pos, int val) {
if (pos < 1)
return head;
// head will change if pos=1
if (pos == 1) {
Node newNode = new Node(val);
newNode.next = head;
return newNode;
}
Node curr = head;
// Traverse to the node that will be
// present just before the new node
for (int i = 1; i < pos - 1 && curr != null; i++) {
curr = curr.next;
}
// If position is greater than the
// number of nodes
if (curr == null)
return head;
Node newNode = new Node(val);
// update the next pointers
newNode.next = curr.next;
curr.next = newNode;
return head;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.val);
if (curr.next != null) {
System.out.print(" -> ");
}
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Creating the list 1->2->4
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(4);
int val = 3, pos = 3;
head = insertPos(head, pos, val);
printList(head);
}}
Python
class Node: def init(self, x): self.val = x self.next = None
def insertPos(head, pos, val):
if pos < 1:
return head
# head will change if pos = 1
if pos == 1:
newNode = Node(val)
newNode.next = head
return newNode
curr = head
# Traverse to the node just before the new node
for i in range(1, pos - 1):
if curr is None:
return head
curr = curr.next
# If position is greater than number of nodes
if curr is None:
return head
newNode = Node(val)
# update the next pointers
newNode.next = curr.next
curr.next = newNode
return headdef printList(head): curr = head while curr: print(curr.val, end="") if curr.next: print(" -> ", end="") curr = curr.next print()
if name == "main": # Creating the list 1->2->4 head = Node(1) head.next = Node(2) head.next.next = Node(4)
val, pos = 3, 3
head = insertPos(head, pos, val)
printList(head)C#
using System;
class Node { public int val; public Node next;
public Node(int x) {
val = x;
next = null;
}}
class GfG {
static Node insertPos(Node head, int pos, int val) {
if (pos < 1)
return head;
// head will change if pos = 1
if (pos == 1) {
Node newNode = new Node(val);
newNode.next = head;
return newNode;
}
Node curr = head;
// Traverse to the node just before the new node
for (int i = 1; i < pos - 1 && curr != null; i++) {
curr = curr.next;
}
// If position is greater than number of nodes
if (curr == null)
return head;
Node newNode2 = new Node(val);
// update the next pointers
newNode2.next = curr.next;
curr.next = newNode2;
return head;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.val);
if (curr.next != null) {
Console.Write(" -> ");
}
curr = curr.next;
}
Console.WriteLine();
}
static void Main(string[] args) {
// Creating the list 1->2->4
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(4);
int val = 3, pos = 3;
head = insertPos(head, pos, val);
printList(head);
}}
JavaScript
class Node { constructor(x) { this.val = x; this.next = null; } }
function insertPos(head, pos, val) {
if (pos < 1) return head;
// head will change if pos = 1
if (pos === 1) {
let newNode = new Node(val);
newNode.next = head;
return newNode;
}
let curr = head;
// Traverse to the node just before the new node
for (let i = 1; i < pos - 1 && curr !== null; i++) {
curr = curr.next;
}
// If position is greater than number of nodes
if (curr === null) return head;
let newNode = new Node(val);
// update the next pointers
newNode.next = curr.next;
curr.next = newNode;
return head;}
function printList(head) { let curr = head; while (curr !== null) { process.stdout.write(curr.val.toString()); if (curr.next !== null) { process.stdout.write(" -> "); } curr = curr.next; } console.log(); }
// Driver code
// Creating the list 1->2->4 let head = new Node(1); head.next = new Node(2); head.next.next = new Node(4);
let val = 3, pos = 3; head = insertPos(head, pos, val); printList(head);
`

