Linked List February 11 ,2026

Deleting a Given Key from a Singly Linked List

Problem Statement

Given the head pointer of a singly linked list and a key value, delete the first node in the linked list whose data matches the given key.

Return the updated head of the linked list.

If the key is not found, the list should remain unchanged.

Understanding the Core Idea

In a singly linked list, each node contains:

  • Data
  • Pointer to the next node

When deleting a node, we must carefully update pointers to maintain the structure of the list.

There are three possible cases:

The node to delete is the head node.
The node to delete is in the middle.
The node to delete is the last node.

Since this is a singly linked list, we cannot move backward. Therefore, while traversing, we must keep track of the previous node.

Algorithm

If head is NULL → return NULL.

If head->data equals key:

  • Store head in a temporary variable.
  • Move head to head->next.
  • Free/Delete the old head.
  • Return new head.

Otherwise:

  • Initialize two pointers:
    • prev = NULL
    • curr = head
  • Traverse until curr is NULL or curr->data equals key.
  • If key not found → return head.
  • Otherwise:
    • prev->next = curr->next
    • Free/Delete curr.
  • 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* deleteKey(struct Node* head, int key) {
    if (head == NULL)
        return NULL;

    // Case 1: head node needs to be deleted
    if (head->data == key) {
        struct Node* temp = head;
        head = head->next;
        free(temp);
        return head;
    }

    struct Node* curr = head;
    struct Node* prev = NULL;

    // Traverse the list
    while (curr != NULL && curr->data != key) {
        prev = curr;
        curr = curr->next;
    }

    // Key not found
    if (curr == NULL)
        return head;

    // Delete node
    prev->next = curr->next;
    free(curr);

    return head;
}

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

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

    head->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->data = 20;

    head->next->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->next->data = 30;
    head->next->next->next = NULL;

    head = deleteKey(head, 20);

    printf("Linked List after deleting key 20: ");
    printList(head);

    return 0;
}

Output:

Linked List after deleting key 20: 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* deleteKey(Node* head, int key) {
    if (head == NULL)
        return NULL;

    // Case 1: Delete head
    if (head->data == key) {
        Node* temp = head;
        head = head->next;
        delete temp;
        return head;
    }

    Node* curr = head;
    Node* prev = NULL;

    // Traverse
    while (curr != NULL && curr->data != key) {
        prev = curr;
        curr = curr->next;
    }

    // Key not found
    if (curr == NULL)
        return head;

    // Delete node
    prev->next = curr->next;
    delete curr;

    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 = deleteKey(head, 20);

    cout << "Linked List after deleting key 20: ";
    printList(head);

    return 0;
}

Output:

Linked List after deleting key 20: 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 deleteKey(Node head, int key) {
        if (head == null)
            return null;

        if (head.data == key) {
            return head.next;
        }

        Node curr = head;
        Node prev = null;

        while (curr != null && curr.data != key) {
            prev = curr;
            curr = curr.next;
        }

        if (curr == null)
            return head;

        prev.next = curr.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 = deleteKey(head, 20);

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

Output:

Linked List after deleting key 20: 10 30

Python Implementation

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

def delete_key(head, key):
    if head is None:
        return None

    if head.data == key:
        return head.next

    curr = head
    prev = None

    while curr and curr.data != key:
        prev = curr
        curr = curr.next

    if curr is None:
        return head

    prev.next = curr.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_key(head, 20)

print("Linked List after deleting key 20:", end=" ")
print_list(head)

Output:

Linked List after deleting key 20: 10 30

 

Next problem in the series:

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

 

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 Node at a...

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