-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_node_constant_time.cpp
More file actions
28 lines (23 loc) · 1.02 KB
/
delete_node_constant_time.cpp
File metadata and controls
28 lines (23 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* @brief Deletes a target node from a singly linked list in O(1) time complexity.
*
* @note This is a standalone helper function. It assumes the address of the target
* node is retrieved externally (e.g., via a pre-implemented `getNode()` method).
*
* @constraints This approach relies on "identity theft" (copying the next node's data).
* Therefore, it CANNOT be used if the target node is the tail of the list.
*/
// The O(1) Deletion Function
void deleteNode(Node* nodeToDelete)
{
// 1. Safety check: Since it's not the tail, nodeToDelete->next exists
if (nodeToDelete == nullptr || nodeToDelete->next == nullptr) { return; }
// 2. Store the pointer to the next node temporarily
Node* temp = nodeToDelete->next;
// 3. Copy the data from the next node to the current node
nodeToDelete->data = temp->data;
// 4. Link the current node to the node after 'temp'
nodeToDelete->next = temp->next;
// 5. Delete the physical memory of the old next node
delete temp;
}