删除链表的节点
发布日期:2021-06-20 02:50:15 浏览次数:6 分类:技术文章

本文共 502 字,大约阅读时间需要 1 分钟。

题目

给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。

返回删除后的链表的头节点。

代码

/** * Definition for singly-linked list. * struct ListNode {
* int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {
} * }; */class Solution {
public: ListNode* deleteNode(ListNode* head, int val) {
ListNode * cur = head; if(head->val == val) return head->next; while(cur->next && cur->next->val != val) cur = cur->next; cur->next = cur->next->next; return head; }};

转载地址:https://blog.csdn.net/free1993/article/details/114918306 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:最小的k个数
下一篇:滑动窗口的最大值

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月08日 15时27分32秒