Leetcode 203 -- Remove Linked List Elements

前言

leetcode的第一道题,值得纪念一下

题目

Remove all elements from a linked list of integers that have value val.

Example
Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6
Return: 1 –> 2 –> 3 –> 4 –> 5

Credits:
Special thanks to @mithmatt for adding this problem and creating all test
cases.

题意

将链表中数值为val的节点全部删除

思路

链表操作基本功,没什么好说的

代码

struct ListNode* removeElements (struct ListNode *head,int val)
{
    struct ListNode *p=(struct ListNode*)malloc(sizeof(struct ListNode));
    p->next=head;
    head = p;
    while(p->next != NULL)
    {
        if(p->next->val==val)
        {
            p->next = p->next->next;
        }
        else
        {
            p = p->next;
        }
    }
    return head->next;
}
如果本文对你有用,可以请作者喝杯茶~
0%