请大家推荐位装修师傅# Living
c*g
1 楼
Given a linked list and a value, delete all the nodes having the value equal
to that value. (Use C++)
class Node
{
public:
int data;
Node* next;
};
void f(int a, Node* head)
{
while(head != NULL)
{
if(head->data == a) // if match, delete the node
{
Node* temp = head;
head->next = head->next->next;
delete temp;
}
head = head->next;
}
}
请问if条件下的作法对吗? 一般来说需要delete那个node吗? (若用java好像就不用)
如果把Node* temp = head;在while loop 之前宣告, 这样会不会比每次都要在if条件
下宣告一次来的好些? 谢谢了
to that value. (Use C++)
class Node
{
public:
int data;
Node* next;
};
void f(int a, Node* head)
{
while(head != NULL)
{
if(head->data == a) // if match, delete the node
{
Node* temp = head;
head->next = head->next->next;
delete temp;
}
head = head->next;
}
}
请问if条件下的作法对吗? 一般来说需要delete那个node吗? (若用java好像就不用)
如果把Node* temp = head;在while loop 之前宣告, 这样会不会比每次都要在if条件
下宣告一次来的好些? 谢谢了