Open In App

C++ Program for Deleting a Node in a Linked List

Last Updated : 26 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Write a C++ program to delete a node from the given link list.

Examples

Input: Linked List: 10 -> 20 -> 30 -> 40 -> 50, Position to delete: 3
Output: 10 -> 20 -> 40 -> 50
Explanation: The node at position 3 is removed. The list then connects node 20 directly to node 40.

Input: Linked List: 5 -> 15 -> 25, Position to delete: 1
Output: 15 -> 25
Explanation: The head node is deleted. The head pointer is updated to point to the next node.

Types of Delete Operation on a Linked List

There are three basic types of delete operations on a singly linked list:

1. Delete the Head Node

When deleting the head node, we need to adjust the head pointer to point to the next node otherwise, the whole linked list can be lost.

Following is the approach to remove the head node of the linked list:

  • Store the current head node in a temporary variable.
  • Move the head pointer to the next node.
  • Delete the temporary node.
delete-singly-linked-list-head-in-cpp

Implementation


Output
Original List: 3 -> 12 -> 15 -> 18 -> NULL
List after deleting head: 12 -> 15 -> 18 -> NULL

Time Complexity: O(1)
Auxiliary Space: O(1)

2. Delete the Tail Node

To delete the tail node, traverse the list to find the second-to-last node, and then set its next pointer to NULL.

Following is the approach to remove the head node of the linked list:

  • Traverse the list to find the second-to-last node.
  • Set the next pointer of the second-to-last node to NULL.
  • Delete the last node.
delete-singly-linked-list-tail-in-cpp

Implementation


Output
Original List: 3 -> 12 -> 15 -> 18 -> NULL
List after deleting head: 3 -> 12 -> 15 -> NULL

Time Complexity: O(N), where N is the number of nodes in the list.
Auxiliary Space: O (1)

3. Delete a Node at a Specific Position

To delete a node at a specific position, traverse the list to reach the node just before the target position, adjust pointers to skip the target node, and delete it.

Following is the approach to remove the head node of the linked list:

  • If the position is 1, delete the head. Otherwise,
  • Traverse the list to reach the node before the target position.
  • Adjust pointers to bypass the target node and point to then next of the target node.
  • Delete the target node.
delete-a-specific-node-from-the-singly-linked-list-in-cpp

Implementation


Output
Original List: 3 -> 12 -> 15 -> 18 -> NULL
List after deleting head: 3 -> 15 -> 18 -> NULL

Time Complexity: O(N), where N is the number of nodes in the list.
Auxiliary Space: O (1)

Conclusion

Deleting a node from a linked list is a common operation that requires careful handling of pointers to avoid breaking the list structure. Depending on the position of the node to be deleted (head, tail, or a specific position), the approach varies slightly. These examples demonstrate how to handle different deletion scenarios in a singly linked list effectively.


Article Tags :
Practice Tags :

Similar Reads