Delete a Node at a given Position in a Linked List in C++

C++ Program to delete a node from a specific position from a Linked List

Let us look at the C++ Program to delete a node from a given position in a Singly Linked List in C++. We will check various methods to delete a specific node in linked list in C++ via which this can be done.

delete node

Steps Involved to delete a specific node in linked list C++

  • Create a linked list with some nodes in it
  • Ask the user for the node he wants to delete
  • Check if the position mentioned doesn’t violate the linked list size and it is a valid position
  • Traverse to the previous nth node to the one user wants to delete.
  • For this (n-1)th node change its next to (n+1)th node
  • Free the memory for the nth node
Delete a specific node in a Linked List in C++

Delete a linked list node at a given position in C++

Run
// Linked List deletion at a given position in C++
#include< iostream>
using namespace std;

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

// Current size of Linked List
int size = 0;

void insert(Node **head, int data)
{
    Node *new_node = new Node();
    new_node->data = data;
    new_node->next = *head;
    *head = new_node;
    ::size++; // Use the global variable 'size'
}

void deletePosition(int pos, Node **head)
{
    Node *temp = *head;
    Node *prevNode;

    if (pos < 1 || pos > ::size) // Use the global variable 'size'
    {
        cout << "Invalid" << endl;
        return;
    }

    // Delete the 1st node
    if (pos == 1)
    {
        *head = (*head)->next;
        cout << temp->data << " deleted" << endl;
        delete temp;
        ::size--; // Use the global variable 'size'
        return;
    }

    // Traverse to the pos'th node
    while (--pos)
    {
        prevNode = temp;
        temp = temp->next;
    }
    
    // Change prevNode node's next node to nth node's next node
    prevNode->next = temp->next;

    // Delete this nth node
    cout << temp->data << " deleted" << endl;
    delete temp;
    ::size--; // Use the global variable 'size'
}

void display(Node *node)
{
    cout << "Linked List: ";

    // As linked list will end when Node is Null
    while (node != NULL)
    {
        cout << node->data << " ";
        node = node->next;
    }
    cout << endl;
}

int main()
{
    Node *head = NULL;

    insert(&head, 100);
    insert(&head, 80);
    insert(&head, 60);
    insert(&head, 40);
    insert(&head, 20);

    display(head);

    // Delete the 2nd node
    deletePosition(2, &head);

    // Delete the 4th node
    deletePosition(4, &head);

    display(head);

    return 0;
}

Output

Linked List : 20 40 60 80 100 
40 deleted
100 deleted
Linked List : 20 60 80

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java