Implement an algorithm in C to delete a node in the middle of a singly linked list.
void delete(Node *head, Node *nodeToDelete) { if (head == nodeToDelete) { head = head->next; } Node *current = head; Node *next = head->next; while (next != NULL) { if (next == nodeToDelete) { current->next = next->next; break; } current = next; next = next->next; } }Followup:
Assume you only have access to that node and not the head of the list
void delete(Node *node) { if (node != NULL) { node->next = node->next->next; node->value = node->next->value; } }
No comments:
Post a Comment