How to delete a node in a linked list at a given Location?

Introducing linked lists and inserting linked lists was covered in a previous post on linked lists of data structures and algorithms.

Let’s create a problem statement to understand the removal process. Specify a “key” to remove the first occurrence of this key in the linked list.

Iterative method:

To remove a node from the linked list, you must perform the following steps:

  1. Find the node before the node you want to remove.
  2. To Change the next of previous node.
  3. Free the memory of the node to delete.


All nodes in the linked list are dynamically allocated using C’s malloc (), so you have to call free () to free the memory allocated to the node you want to remove.

For example:




Here Code:

#include <bits/stdc++.h>
using namespace std;
class Node{
          int data;
          Node* next;
void push(Node** head_ref, int new_data)
          Node* new_node = new Node();
          new_node->data = new_data;
          new_node->next = (*head_ref);
          (*head_ref) = new_node;
void deleteNode(Node** head_ref, int key)
          Node* temp = *head_ref;
          Node* prev = NULL;
          if (temp != NULL && temp->data == key)
                   *head_ref = temp->next;
                   delete temp;                 
          while (temp != NULL && temp->data != key)
                   prev = temp;
                   temp = temp->next;
          if (temp == NULL)
          prev->next = temp->next;
          delete temp;
void printList(Node* node)
          while (node != NULL)
                   cout << node->data << " ";
                   node = node->next;
int main()
          Node* head = NULL;
          push(&head, 7);
          push(&head, 1);
          push(&head, 3);
          push(&head, 2);
          puts("Created Linked List: ");
          deleteNode(&head, 1);
          puts("\nLinked List after Deletion of 1: ");
          return 0;


In this article, you will learn how to delete a node in a linked list at a specific location.

Recommended Articles:

  1. How to split a circular linked list into two halves?
  2. How to reverse a doubly linked list?
  3. How to reverse a string using stack?
  4. How to swap nodes in a linked list without Swapping data?
  5. How to write C functions that modify head pointer of a Linked List?


Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 73 times, 1 visits today)

0 0 vote
Article Rating
Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments

[…] How to delete a node in a linked list at a given Location? […]

You cannot copy content of this page
Scroll Up
Would love your thoughts, please comment.x

  • Name

  • Minimum length of 8 characters.