题解 | #删除链表的节点#
删除链表的节点
https://www.nowcoder.com/practice/f9f78ca89ad643c99701a7142bd59f5d
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param val int整型
* @return ListNode类
*/
struct ListNode* deleteNode(struct ListNode* head, int val ) {
// write code here
struct ListNode *dummyHead = (struct ListNode*)malloc(sizeof(struct ListNode));//申请内存
dummyHead->next = head;
struct ListNode *cur = dummyHead;
while(cur->next != NULL){
if(cur->next->val == val){
struct ListNode *tmp = (struct ListNode*)malloc(sizeof(struct ListNode));
tmp = cur->next;
cur->next = cur->next->next;
free(tmp);//释放内存
}
else cur = cur->next;
}
head = dummyHead->next;
free(dummyHead);
return head;
}
腾讯成长空间 1155人发布