题解 | #二叉树中和为某一值的路径#

删除链表的节点

http://www.nowcoder.com/practice/f9f78ca89ad643c99701a7142bd59f5d

删除链表某一个节点,只需要注意 1.链表为空 2.删除的节点是头节点 3.删除的节点在链表中间

 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 *	ListNode(int x) : val(x), next(nullptr) {}
 * };
 */
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @param val int整型 
     * @return ListNode类
     */
    ListNode* deleteNode(ListNode* head, int val) {
        // write code here
        ListNode *cur=new ListNode(-1);
        cur->next=head;
        ListNode *res=cur;//指向cur的初始位置
        while(cur->next){//cur指针不断移动
            if(cur->next->val==val){
                ListNode *temp=cur->next->next;
                cur->next=temp;
            }
            cur=cur->next;
        }
        return res->next;
    }
};
全部评论
ListNode *temp=cur->next->next; cur->next=temp; 可以修改为 ListNode *temp=cur->next->next; delete cur->next; cur->next=temp;
点赞 回复 分享
发布于 2021-11-04 11:18

相关推荐

12-13 20:26
浙江大学 Java
淬月星辉:把浙大的校名加大加粗,把校徽再贴出来,就OK了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务