VOOZH about

URL: https://www.geeksforgeeks.org/dsa/insert-a-node-at-a-specific-position-in-a-linked-list/

⇱ Insert a node at a specific position in a linked list - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Insert a node at a specific position in a linked list

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

👁 Screenshot-2025-09-02-102354

Output: 1 -> 2 -> 3 -> 4
Explanation: Node inserted at position 3.

👁 frame_3090

[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.

Output
1 -> 2 -> 3 -> 4
Comment
Article Tags:
Article Tags: