更新时间:2023-01-17 18:10:41
有两种方法.
如果在通过值传递指向头节点的指针时使用您的方法,则该函数可以采用以下方式.
If to use your approach when the pointer to the head node is passed by value then the function can look the following way.
node * delete_even_node( node *head )
{
while ( head && head->data % 2 == 0 )
{
node *tmp = head;
head = head->next;
free( tmp );
}
if ( head )
{
node *current = head;
while ( current->next )
{
if ( current->next->data % 2 == 0 )
{
node *tmp = current->next;
current->next = current->next->next;
free( tmp );
}
else
{
current = current->next;
}
}
}
return head;
}
另一种方法是通过引用将指针传递到头节点.
Another approach is to pass the pointer to the head node by reference.
例如
void delete_even_node( node **head )
{
while ( *head )
{
if ( ( *head )->data % 2 == 0 )
{
node *tmp = *head;
*head = ( *head )->next;
free( tmp );
}
else
{
head = &( *head )->next;
}
}
}
该函数的调用方式类似于
And the function is called like
delete_even_node( &head );