
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Delete a Linked List node at a given position in C++
In this tutorial, we are going to learn how to delete a node in singly linked list with the given position.
Let's see the steps to solve the problem.
Write struct with data, and next pointer.
Write a function to insert the node into the singly linked list.
Initialize the singly linked list with dummy data.
Initialize the position to delete the node.
Iterate over the linked list and find the node with the given position to delete the node.
Write a function to delete the node. Consider the following three cases while deleting the node.
If the node is head node, then move the head to next node.
If the node is middle node, then link the next node to the previous node
If the node is end node, then remove the previous node link.
Example
Let's see the code
#include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node *next; }; void insertNode(struct Node** head_ref, int new_data) { struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } void deleteNode(struct Node **head_ref, int position) { if (*head_ref == NULL) { return; } struct Node* temp = *head_ref; if (position == 1) { *head_ref = temp->next; free(temp); return; } for (int i = 2; temp != NULL && i < position - 1; i++) { temp = temp->next; } if (temp == NULL || temp->next == NULL) { return; } struct Node *next = temp->next->next; free(temp->next); temp->next = next; } void printLinkedList(struct Node *node) { while (node != NULL) { cout << node->data << "->"; node = node->next; } } int main() { struct Node* head = NULL; insertNode(&head, 1); insertNode(&head, 2); insertNode(&head, 3); insertNode(&head, 4); insertNode(&head, 5); cout << "Linked list before deletion:" << endl; printLinkedList(head); deleteNode(&head, 1); cout << "\nLinked list after deletion:" << endl; printLinkedList(head); return 0; }
Output
If you execute the above program, then you will get the following result.
Linked list before deletion: 5->4->3->2->1-> Linked list after deletion: 4->3->2->1->
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Delete a Doubly Linked List node at a given position in C++
- Delete Node in a Linked List in Python
- Delete a node in a Doubly Linked List in C++
- Delete a tail node from the given singly Linked List using C++
- C++ Program to Delete the First Node in a given Singly Linked List
- C# Program to add a node at the first position in a Linked List
- C# Program to add a node at the last position in a Linked List
- C program to insert a node at any position using double linked list
- Golang Program to delete the ith index node, when the index is at 0th position in the linked list.
- Delete a Node from linked list without head pointer in C++
- Golang Program to add a node at the end of a given linked list.
- Golang Program to delete the first node from a linked list.
- Golang Program to delete the last node from a linked list.
- C# Program to add a node after the given node in a Linked List
- C# Program to add a node before the given node in a Linked List
