链接:
剑指 Offer 18. 删除链表的节点
题意:
如题
解:
基础链表操作
实际代码:
#include<iostream>
using namespace std;
struct ListNode
{int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};
ListNode* deleteNode(ListNode* head, int val)
{ListNode* ret=head;if(head->val==val) return head->next;while(head!=nullptr){if(head->next->val==val){head->next=head->next->next;break;}else head=head->next;}return ret;
}
int main()
{}
限制:
- 题目保证链表中节点的值互不相同
- 若使用 C 或 C++ 语言,你不需要
free
或delete
被删除的节点