Delete Node by Position

Last Updated : 3 Aug, 2026

Given the head of a linked list and an integer x, delete the node at position x and return the updated head of the linked list.

Note: Positions use 1-based indexing.

Examples:  

Input: x = 4,

8

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

9


Input: x = 6,

10

Output: 2 -> 5 -> 7 -> 8 -> 99
Explanation: After deleting the node at 6th position, the linked list is as

11
Try It Yourself
redirect icon

[Naive Approach] Store Nodes in an Array - O(n) Time and O(n) Space

The idea is to traverse the linked list and store the address of every node in an array. This allows direct access to the node at position x. Then, update the links to remove the node and delete it.

Working of Approach:

  • Traverse the linked list and store all node pointers in an array.
  • If x == 1, delete the head node.
  • Otherwise, access the previous and current nodes using the array.
  • Update the previous node's next pointer.
  • Delete the node and return the updated head.
C++
#include <bits/stdc++.h>
using namespace std;

class Node
{
  public:
    int data;
    Node *next;
    Node(int x)
    {
        data = x;
        next = nullptr;
    }
};

Node *deleteNode(Node *head, int x)
{

    vector<Node *> nodes;

    // Store all node pointers
    Node *curr = head;
    while (curr != nullptr)
    {
        nodes.push_back(curr);
        curr = curr->next;
    }

    // Delete the head node
    if (x == 1)
    {
        Node *temp = head;
        head = head->next;
        delete temp;
        return head;
    }

    Node *prev = nodes[x - 2];
    Node *delNode = nodes[x - 1];

    // Remove the node
    prev->next = delNode->next;
    delete delNode;

    return head;
}

// Function to print the linked list
void printList(Node *head)
{
    while (head != nullptr)
    {
        cout << head->data;
        if (head->next)
            cout << " -> ";
        head = head->next;
    }
    cout << endl;
}

int main()
{

    // Create linked list:
    // 2 -> 5 -> 7 -> 8 -> 99 -> 100
    Node *head = new Node(2);
    head->next = new Node(5);
    head->next->next = new Node(7);
    head->next->next->next = new Node(8);
    head->next->next->next->next = new Node(99);
    head->next->next->next->next->next = new Node(100);

    int x = 6;

    head = deleteNode(head, x);

    printList(head);

    return 0;
}
Java
import java.util.ArrayList;

class Node {
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

public class GFG {

    public static Node deleteNode(Node head, int x)
    {
        ArrayList<Node> nodes = new ArrayList<>();

        // Store all node pointers
        Node curr = head;
        while (curr != null) {
            nodes.add(curr);
            curr = curr.next;
        }

        // Delete the head node
        if (x == 1) {
            Node temp = head;
            head = head.next;
            temp = null;
            return head;
        }

        Node prev = nodes.get(x - 2);
        Node delNode = nodes.get(x - 1);

        // Remove the node
        prev.next = delNode.next;
        delNode = null;

        return head;
    }

    // Function to print the linked list
    public static void printList(Node head)
    {
        while (head != null) {
            System.out.print(head.data);
            if (head.next != null)
                System.out.print(" -> ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args)
    {

        // Create linked list:
        // 2 -> 5 -> 7 -> 8 -> 99 -> 100
        Node head = new Node(2);
        head.next = new Node(5);
        head.next.next = new Node(7);
        head.next.next.next = new Node(8);
        head.next.next.next.next = new Node(99);
        head.next.next.next.next.next = new Node(100);

        int x = 6;

        head = deleteNode(head, x);

        printList(head);
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None

def deleteNode(head, x):
    nodes = []

    # Store all node pointers
    curr = head
    while curr is not None:
        nodes.append(curr)
        curr = curr.next

    # Delete the head node
    if x == 1:
        temp = head
        head = head.next
        del temp
        return head

    prev = nodes[x - 2]
    delNode = nodes[x - 1]

    # Remove the node
    prev.next = delNode.next
    del delNode

    return head

# Function to print the linked list
def printList(head):
    while head is not None:
        print(head.data, end="")
        if head.next is not None:
            print(" -> ", end="")
        head = head.next
    print()

if __name__ == "__main__":

    # Create linked list:
    # 2 -> 5 -> 7 -> 8 -> 99 -> 100
    head = Node(2)
    head.next = Node(5)
    head.next.next = Node(7)
    head.next.next.next = Node(8)
    head.next.next.next.next = Node(99)
    head.next.next.next.next.next = Node(100)

    x = 6

    head = deleteNode(head, x)

    printList(head)
C#
using System;
using System.Collections.Generic;

public class Node {
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

public class GFG {
    public static Node deleteNode(Node head, int x)
    {
        List<Node> nodes = new List<Node>();

        // Store all node pointers
        Node curr = head;
        while (curr != null) {
            nodes.Add(curr);
            curr = curr.next;
        }

        // Delete the head node
        if (x == 1) {
            Node temp = head;
            head = head.next;
            temp = null;
            return head;
        }

        Node prev = nodes[x - 2];
        Node delNode = nodes[x - 1];

        // Remove the node
        prev.next = delNode.next;
        delNode = null;

        return head;
    }

    // Function to print the linked list
    public static void printList(Node head)
    {
        while (head != null) {
            Console.Write(head.data);
            if (head.next != null)
                Console.Write(" -> ");
            head = head.next;
        }
        Console.WriteLine();
    }

    public static void Main()
    {
        // Create linked list:
        // 2 -> 5 -> 7 -> 8 -> 99 -> 100
        Node head = new Node(2);
        head.next = new Node(5);
        head.next.next = new Node(7);
        head.next.next.next = new Node(8);
        head.next.next.next.next = new Node(99);
        head.next.next.next.next.next = new Node(100);

        int x = 6;

        head = deleteNode(head, x);

        printList(head);
    }
}
JavaScript
class Node {
    constructor(x)
    {
        this.data = x;
        this.next = null;
    }
}

function deleteNode(head, x)
{
    let nodes = [];

    // Store all node pointers
    let curr = head;
    while (curr !== null) {
        nodes.push(curr);
        curr = curr.next;
    }

    // Delete the head node
    if (x === 1) {
        let temp = head;
        head = head.next;
        temp = null;
        return head;
    }

    let prev = nodes[x - 2];
    let delNode = nodes[x - 1];

    // Remove the node
    prev.next = delNode.next;
    delNode = null;

    return head;
}

// Function to print the linked list
function printList(head)
{
    while (head !== null) {
        process.stdout.write(head.data.toString());
        if (head.next !== null)
            process.stdout.write(" -> ");
        head = head.next;
    }
    console.log();
}

// Driver Code
// Create linked list:
// 2 -> 5 -> 7 -> 8 -> 99 -> 100
let head = new Node(2);
head.next = new Node(5);
head.next.next = new Node(7);
head.next.next.next = new Node(8);
head.next.next.next.next = new Node(99);
head.next.next.next.next.next = new Node(100);

let x = 6;
head = deleteNode(head, x);
printList(head);

Output
2 -> 5 -> 7 -> 8 -> 99

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

The idea is to traverse the linked list only once while keeping track of the previous node. When the node at position x is reached, update the previous node's next pointer to skip it and delete the node. If x is 1, simply update the head pointer.

Working of Approach:

  • If x == 1, delete the head node.
  • Traverse the list while maintaining both current and previous pointers.
  • Stop when the current node reaches position x.
  • Update the previous node's next pointer.
  • Delete the current node and return the updated head.

Let us understand with an example:
Input: x = 6,

10
  • The linked list is 2 -> 5 -> 7 -> 8 -> 99 -> 100 and x = 6, so the head is not deleted.
  • Traverse the list while updating prev and temp. After the loop, prev points to 99 and temp points to 100.
  • Update prev->next to temp->next (nullptr), removing 100 from the linked list.
  • Delete the node pointed to by temp.
  • The updated linked list becomes
11


C++
#include <bits/stdc++.h>
using namespace std;

class Node
{
  public:
    int data;
    Node *next;
    Node(int x)
    {
        data = x;
        next = nullptr;
    }
};

Node *deleteNode(Node *head, int x)
{
    Node *temp = head;

    // Case 1: Head is to be deleted
    if (x == 1)
    {
        head = temp->next;
        delete temp;
        return head;
    }

    // Case 2: Traverse to the node
    // before the one to be deleted
    Node *prev = nullptr;
    for (int i = 1; i < x; i++)
    {
        prev = temp;
        temp = temp->next;
    }

    // Delete the node at position x
    prev->next = temp->next;
    delete temp;

    return head;
}

// Function to print the linked list
void printList(Node *head)
{
    while (head != nullptr)
    {
        cout << head->data;
        if (head->next)
            cout << " -> ";
        head = head->next;
    }
    cout << endl;
}

int main()
{

    // Create linked list:
    // 2 -> 5 -> 7 -> 8 -> 99 -> 100
    Node *head = new Node(2);
    head->next = new Node(5);
    head->next->next = new Node(7);
    head->next->next->next = new Node(8);
    head->next->next->next->next = new Node(99);
    head->next->next->next->next->next = new Node(100);

    int x = 6;

    head = deleteNode(head, x);

    printList(head);

    return 0;
}
Java
class Node {
    public int data;
    public Node next;
    public Node(int x)
    {
        data = x;
        next = null;
    }
}

public class GFG {
    public static Node deleteNode(Node head, int x)
    {
        Node temp = head;

        // Case 1: Head is to be deleted
        if (x == 1) {
            head = temp.next;
            temp = null;
            return head;
        }

        // Case 2: Traverse to the node
        // before the one to be deleted
        Node prev = null;
        for (int i = 1; i < x; i++) {
            prev = temp;
            temp = temp.next;
        }

        // Delete the node at position x
        prev.next = temp.next;
        temp = null;

        return head;
    }

    public static void printList(Node head)
    {
        while (head != null) {
            System.out.print(head.data);
            if (head.next != null)
                System.out.print(" -> ");
            head = head.next;
        }
        System.out.println();
    }

    public static void main(String[] args)
    {

        // Create linked list:
        // 2 -> 5 -> 7 -> 8 -> 99 -> 100
        Node head = new Node(2);
        head.next = new Node(5);
        head.next.next = new Node(7);
        head.next.next.next = new Node(8);
        head.next.next.next.next = new Node(99);
        head.next.next.next.next.next = new Node(100);

        int x = 6;

        head = deleteNode(head, x);

        printList(head);
    }
}
Python
class Node:
  def __init__(self, x):
    self.data = x
    self.next = None

def deleteNode(head, x):
  temp = head

  # Case 1: Head is to be deleted
  if x == 1:
    head = temp.next
    del temp
    return head

  # Case 2: Traverse to the node
  # before the one to be deleted
  prev = None
  for i in range(1, x):
    prev = temp
    temp = temp.next

  # Delete the node at position x
  prev.next = temp.next
  del temp

  return head

def printList(head):
  while head is not None:
    print(head.data, end='')
    if head.next is not None:
      print(' -> ', end='')
    head = head.next
  print()

if __name__ == '__main__':

  # Create linked list:
  # 2 -> 5 -> 7 -> 8 -> 99 -> 100
  head = Node(2)
  head.next = Node(5)
  head.next.next = Node(7)
  head.next.next.next = Node(8)
  head.next.next.next.next = Node(99)
  head.next.next.next.next.next = Node(100)

  x = 6

  head = deleteNode(head, x)

  printList(head)
C#
class Node {
    public int data;
    public Node next;
    public Node(int x)
    {
        data = x;
        next = null;
    }
}

class GFG {
    static Node deleteNode(Node head, int x)
    {
        Node temp = head;

        // Case 1: Head is to be deleted
        if (x == 1) {
            head = temp.next;
            temp = null;
            return head;
        }

        // Case 2: Traverse to the node
        // before the one to be deleted
        Node prev = null;
        for (int i = 1; i < x; i++) {
            prev = temp;
            temp = temp.next;
        }

        // Delete the node at position x
        prev.next = temp.next;
        temp = null;

        return head;
    }

    static void printList(Node head)
    {
        while (head != null) {
            System.Console.Write(head.data);
            if (head.next != null)
                System.Console.Write(" -> ");
            head = head.next;
        }
        System.Console.WriteLine();
    }

    static void Main(string[] args)
    {

        // Create linked list:
        // 2 -> 5 -> 7 -> 8 -> 99 -> 100
        Node head = new Node(2);
        head.next = new Node(5);
        head.next.next = new Node(7);
        head.next.next.next = new Node(8);
        head.next.next.next.next = new Node(99);
        head.next.next.next.next.next = new Node(100);

        int x = 6;

        head = deleteNode(head, x);

        printList(head);
    }
}
JavaScript
class Node {
    constructor(x)
    {
        this.data = x;
        this.next = null;
    }
}

function deleteNode(head, x)
{
    let temp = head;

    // Case 1: Head is to be deleted
    if (x === 1) {
        head = temp.next;
        temp = null;
        return head;
    }

    // Case 2: Traverse to the node
    // before the one to be deleted
    let prev = null;
    for (let i = 1; i < x; i++) {
        prev = temp;
        temp = temp.next;
    }

    // Delete the node at position x
    prev.next = temp.next;
    temp = null;

    return head;
}

function printList(head)
{
    while (head !== null) {
        process.stdout.write(head.data.toString());
        if (head.next !== null) {
            process.stdout.write(" -> ");
        }
        head = head.next;
    }
    console.log();
}

// Driver Code
// Create linked list:
// 2 -> 5 -> 7 -> 8 -> 99 -> 100
let head = new Node(2);
head.next = new Node(5);
head.next.next = new Node(7);
head.next.next.next = new Node(8);
head.next.next.next.next = new Node(99);
head.next.next.next.next.next = new Node(100);

let x = 6;
head = deleteNode(head, x);
printList(head);

Output
2 -> 5 -> 7 -> 8 -> 99
Comment