就是很基本的删除链表中值为val的结点,这是一道简单题,然而我一开始却没有写对...
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val)
{
struct ListNode* p = (struct ListNode*)malloc(sizeof(struct ListNode));//struct ListNode* p = malloc(sizeof(struct ListNode));也有这种写法
p->next = head;
struct ListNode* temp = p;
while (temp->next != NULL)
{
if (temp->next->val == val)
{
temp->next = temp->next->next;
}
else
{
temp = temp->next;
}
}
return p->next;
}