码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode203. 移除链表元素

时间:2018-12-06 01:26:54      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:efi   示例   输入   color   span   div   head   class   init   

删除链表中等于给定值 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) {
    while(head && head->val == val)
    {
        struct ListNode* tmp = head;
        head = head->next;
        free(tmp);
    }
    if(head == NULL) return head;
    struct ListNode* cur = head;
    while(cur->next)
    {
        if(cur->next->val == val)
        {
            struct ListNode* tmp = cur->next;
            cur->next = cur->next->next;  
            free(tmp);
        }
        else
        {
            cur = cur->next;
        }
    }
    return head;
}

 

leetcode203. 移除链表元素

标签:efi   示例   输入   color   span   div   head   class   init   

原文地址:https://www.cnblogs.com/xinfenglee/p/10074295.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!