
- 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 Node from linked list without head pointer in C++
In this tutorial, we are going to learn how to delete a node without head pointer in a singly linked list.
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.
Take a node from the linked list using the next pointer.
Move the delete node to the next node.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; struct Node { int data; struct Node* next; }; void deleteNodeWithoutHead(struct Node* deletingNode) { if (deletingNode == NULL) { return; } else { if (deletingNode->next == NULL) { cout << "Can't delete last node without head" << endl; return; } struct Node* temp = deletingNode->next; deletingNode->data = temp->data; deletingNode->next = temp->next; free(temp); } } void printLinkedList(Node* head) { Node* temp = head; while (temp) { cout << temp->data << " -> "; temp = temp->next; } } void insertNode(struct Node** head_ref, int new_data) { struct Node* new_node = new Node(); new_node->data = new_data; new_node->next = (*head_ref); (*head_ref) = new_node; } int main() { struct Node* head = NULL; insertNode(&head, 1); insertNode(&head, 2); insertNode(&head, 3); insertNode(&head, 4); insertNode(&head, 5); insertNode(&head, 6); cout << "Linked List before deletion:" << endl; printLinkedList(head); Node* del = head->next; deleteNodeWithoutHead(del); cout << "\nLinked List after deletion:" << endl; printLinkedList(head); return 0; }
Output
If you run the above code, then you will get the following result.
Linked List before deletion: 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> Linked List after deletion: 6 -> 4 -> 3 -> 2 -> 1 ->
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Find kth node from Middle towards Head of a Linked List in C++
- Delete a node in a Doubly Linked List in C++
- Delete a tail node from the given singly Linked List using C++
- Delete Node in a Linked List in Python
- Delete a Linked List node at a given position in C++
- Golang Program to delete the first node from a linked list.
- Golang Program to delete the last node from a linked list.
- Delete a Doubly Linked List node at a given position in C++
- C++ Program to Delete the First Node in a given Singly Linked List
- Point arbit pointer to greatest value right side node in a linked list in C++
- Point to next higher value node in a linked list with an arbitrary pointer in C++
- Python program to delete a node from the end of the Circular Linked List
- Python program to delete a node from the middle of the Circular Linked List
- Linked List Random Node in C++
- Python program to delete a new node from the beginning of the doubly linked list

Advertisements