How do you delete a given node from a single linked list?
Anónimo
template class LList { public: T val; LList* next; public: static void Delete(LList** head, T& node) { if(NULL == (*head)) return; LList* next; // the case when the element to delete is a head of the list if((*head)->val == node) { next = (*head)->next; delete (*head); (*head) = next; } else { LList* cur = *head; while(cur->next) { if(node == cur->next->val) { next = cur->next->next; delete cur->next; cur->next = next; break; } cur = cur->next; } } } };