当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 【leetcode 简单】 第五十七题 删除链表中的节点

【leetcode 简单】 第五十七题 删除链表中的节点

2018年08月22日  | 移动技术网IT编程  | 我要评论

搞定岳父大人迅雷下载,富联网官网,世界杯比分

删除链表中等于给定值 val 的所有节点。

示例:

输入: 1->2->6->3->4->5->6, val = 6
输出: 1->2->3->4->5


/**
 * definition for singly-linked list.
 * struct listnode {
 *     int val;
 *     struct listnode *next;
 * };
 */

struct listnode* removeelements(struct listnode* head, int val) {
    struct listnode* s = (struct listnode* )malloc(sizeof(struct listnode));
    s->next = head;
    
    struct listnode* pre = s;
    struct listnode* cur = head;
    while (cur)
    {
        if (cur->val == val)
        {
            pre->next = cur->next;
        }
        else
        {
            pre = cur;
        }
        cur = cur->next;
    }
    return s->next;
}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网