interactive-tutorials
interactive-tutorials copied to clipboard
Maybe slightly wrong solution in learn-c.org with Linked Lists
I think the solution differs from the exercise, in the case as the solution would search and delete all nodes with the value of "val" instead of deleting only the first node with this value. Also, the additional function is confusing in the solution since it's never used.
Second: Please explain a little bit closer the use of a pointer to a pointer. With a nice graphic like in: https://www.cplusplus.com/doc/tutorial/pointers/
Please verify this. Up to that, Great Tutorial! I love it :)
Maybe the solution to it:
int remove_by_value(node_t ** head, int val) {
int retval = -1;
node_t * temp_node = NULL;
node_t * current_node = *head;
if(*head == NULL) return -1; // if list is empty
if((*head) -> val == val) return pop(head); // if first element has "val"
while(current_node -> next -> val != val){
current_node = current_node -> next; }
temp_node = current_node -> next;
current_node -> next = temp_node -> next;
retval = temp_node -> val;
free(temp_node);
}