VOOZH about

URL: https://www.geeksforgeeks.org/dsa/delete-a-linked-list-node-at-a-given-position/

⇱ Delete Node by Position - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Delete Node by Position

Last Updated : 29 Aug, 2025

Given the head of a singly linked list and a position (1-based index), delete the node at that position and return the updated head of the linked list.

Note: Position will be valid (i.e, 1 <= position <= linked list length)

Example:

Input: position = 1

👁 blobid0_1755951344

Output: 2-> 3 -> 1-> 7 -> nullptr
Explanation: After deleting the node at the 1st position (1-base indexing), the linked list is as

👁 blobid1_1755951379

Input: position = 5

👁 blobid5_1755951502

Output: 2 -> 3 -> 4 -> 5 -> nullptr
Explanation: After deleting the node at 5th position (1-based indexing), the linked list is as

👁 blobid4_1755951496

[Approach] Single Traversal Deletion - O(n) Time and O(1) Space

Deletion at a specified position in a linked list involves removing a node from a specific index/position, which can be the first, middle, or last node.

To perform the deletion, If the position is 1, we update the head to point to the next node and delete the current head. For other positions, we traverse the list to reach the node just before the specified position. If the target node exists, we adjust the next of this previous node to point to next of next nodes, which will result in skipping the target node.


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