Linked List February 11 ,2026

Deleting a Node at a Given Position in a Singly Linked List

Problem Statement

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

Return the updated head of the linked list.

If the position is invalid (greater than the length of the list or less than 1), the list should remain unchanged.

Understanding the Logic

In a singly linked list, deletion requires careful pointer manipulation.

There are two major scenarios:

  • Deleting the first node (position = 1)
  • Deleting any other node (middle or last)

Because this is a singly linked list, we cannot move backward. Therefore, we must:

  • Traverse until position - 1
  • Maintain a previous pointer
  • Update the next reference properly

Algorithm

If head is NULL → return NULL.

If position == 1:

  • Store head in temp
  • Move head = head->next
  • Free/Delete temp
  • Return head

Otherwise:

  • Traverse until position - 1
  • If traversal ends before reaching position - 1 → invalid position
  • Store node to delete in temp
  • Update prev->next = temp->next
  • Free/Delete temp
  • Return head

Time and Space Complexity

Time Complexity: O(n)
Space Complexity: O(1)

C Implementation

// While running this code, replace all #include "..." with #include <...>

#include "stdio.h"
#include "stdlib.h"

struct Node {
    int data;
    struct Node* next;
};

struct Node* deleteAtPosition(struct Node* head, int position) {
    if (head == NULL)
        return NULL;

    if (position == 1) {
        struct Node* temp = head;
        head = head->next;
        free(temp);
        return head;
    }

    struct Node* prev = head;

    for (int i = 1; i < position - 1 && prev != NULL; i++) {
        prev = prev->next;
    }

    if (prev == NULL || prev->next == NULL)
        return head;

    struct Node* temp = prev->next;
    prev->next = temp->next;
    free(temp);

    return head;
}

void printList(struct Node* head) {
    while (head != NULL) {
        printf("%d ", head->data);
        head = head->next;
    }
}

int main() {
    struct Node* head = malloc(sizeof(struct Node));
    head->data = 10;
    head->next = malloc(sizeof(struct Node));
    head->next->data = 20;
    head->next->next = malloc(sizeof(struct Node));
    head->next->next->data = 30;
    head->next->next->next = NULL;

    head = deleteAtPosition(head, 2);

    printf("Linked List after deleting position 2: ");
    printList(head);

    return 0;
}

Output:

Linked List after deleting position 2: 10 30

C++ Implementation


// While running this code, replace all #include "..." with #include <...>

#include "iostream"
using namespace std;

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

    Node(int val) {
        data = val;
        next = NULL;
    }
};

Node* deleteAtPosition(Node* head, int position) {
    if (head == NULL)
        return NULL;

    if (position == 1) {
        Node* temp = head;
        head = head->next;
        delete temp;
        return head;
    }

    Node* prev = head;

    for (int i = 1; i < position - 1 && prev != NULL; i++) {
        prev = prev->next;
    }

    if (prev == NULL || prev->next == NULL)
        return head;

    Node* temp = prev->next;
    prev->next = temp->next;
    delete temp;

    return head;
}

void printList(Node* head) {
    while (head != NULL) {
        cout << head->data << " ";
        head = head->next;
    }
}

int main() {
    Node* head = new Node(10);
    head->next = new Node(20);
    head->next->next = new Node(30);

    head = deleteAtPosition(head, 2);

    cout << "Linked List after deleting position 2: ";
    printList(head);

    return 0;
}

Output:

Linked List after deleting position 2: 10 30

Java Implementation

class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class Main {

    public static Node deleteAtPosition(Node head, int position) {
        if (head == null)
            return null;

        if (position == 1)
            return head.next;

        Node temp = head;

        for (int i = 1; i < position - 1 && temp != null; i++) {
            temp = temp.next;
        }

        if (temp == null || temp.next == null)
            return head;

        temp.next = temp.next.next;

        return head;
    }

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

    public static void main(String[] args) {
        Node head = new Node(10);
        head.next = new Node(20);
        head.next.next = new Node(30);

        head = deleteAtPosition(head, 2);

        System.out.print("Linked List after deleting position 2: ");
        printList(head);
    }
}

Output:

Linked List after deleting position 2: 10 30

Python Implementation

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def delete_at_position(head, position):
    if head is None:
        return None

    if position == 1:
        return head.next

    temp = head
    for _ in range(position - 2):
        if temp is None:
            return head
        temp = temp.next

    if temp is None or temp.next is None:
        return head

    temp.next = temp.next.next

    return head

def print_list(head):
    while head:
        print(head.data, end=" ")
        head = head.next
    print()

head = Node(10)
head.next = Node(20)
head.next.next = Node(30)

head = delete_at_position(head, 2)

print("Linked List after deleting position 2:", end=" ")
print_list(head)

Output:

Linked List after deleting position 2: 10 30

 

Next problem in the  series:

 Delete Entire Linked List in Data Structures

 

Sanjiv
0

You must logged in to post comments.

Related Blogs

Introducti...
Linked List January 01 ,2026

Introduction to Link...

Length of...
Linked List January 01 ,2026

Length of a Singly L...

Remove Eve...
Linked List January 01 ,2026

Remove Every K-th No...

Middle of...
Linked List January 01 ,2026

Middle of a Linked L...

Count Occu...
Linked List January 01 ,2026

Count Occurrences in...

Circular L...
Linked List January 01 ,2026

Circular Linked List...

Check if a...
Linked List January 01 ,2026

Check if a Linked Li...

Count Node...
Linked List January 01 ,2026

Count Nodes in a Cir...

Deletion f...
Linked List January 01 ,2026

Deletion from a Circ...

Singly to...
Linked List January 01 ,2026

Singly to Circular L...

Exchange F...
Linked List January 01 ,2026

Exchange First and L...

Deletion i...
Linked List January 01 ,2026

Deletion in a Doubly...

Print a Si...
Linked List February 02 ,2026

Print a Singly Linke...

Search in...
Linked List February 02 ,2026

Search in a Singly L...

Linked Lis...
Linked List February 02 ,2026

Linked List Insertio...

Deleting a...
Linked List February 02 ,2026

Deleting a Given Key...

Delete Ent...
Linked List February 02 ,2026

Delete Entire Linked...

Nth Node f...
Linked List February 02 ,2026

Nth Node from Start

Nth Node f...
Linked List February 02 ,2026

Nth Node from End

Size of Do...
Linked List February 02 ,2026

Size of Doubly Linke...

Reverse a...
Linked List February 02 ,2026

Reverse a Singly Lin...

Reverse a...
Linked List February 02 ,2026

Reverse a Doubly Lin...

Insert at...
Linked List February 02 ,2026

Insert at Beginning...

Insert at...
Linked List February 02 ,2026

Insert at End of Dou...

Delete Fir...
Linked List February 02 ,2026

Delete First Node of...

Check if L...
Linked List February 02 ,2026

Check if Linked List...

Copy a Lin...
Linked List February 02 ,2026

Copy a Linked List

Swap Nodes...
Linked List February 02 ,2026

Swap Nodes in Pairs

Detect Loo...
Linked List February 02 ,2026

Detect Loop in a Lin...

Length of...
Linked List February 02 ,2026

Length of Loop in Li...

Design Bro...
Linked List February 02 ,2026

Design Browser Histo...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com