struct ListNode* removeElements(struct ListNode* head, int val) { typedefstructListNodeListNode; ListNode* pre = (ListNode*)malloc(sizeof(ListNode)); if (pre == NULL){//判断pre是否为空,若为空则直接返回报错。 return pre; } pre->next = head;// point to head ListNode* cur = pre; while (cur->next){ if (cur->next->val == val) { ListNode* temp = cur->next; cur->next = cur->next->next; free(temp); } else { cur = cur->next; } } head = pre->next;// delete old head ,pre will point new head auto. return head; }