鱼C论坛

 找回密码
 立即注册
查看: 1668|回复: 0

[吹水] LeetCode-203

[复制链接]
发表于 2023-7-19 15:06:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 yinda_peng 于 2023-7-19 15:08 编辑

203. Remove Linked List Elements    (easy)

Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.


Example 1:
Input: head = [1,2,6,3,4,5,6], val = 6
Output: [1,2,3,4,5]
Example 2:
Input: head = [], val = 1
Output: []
Example 3:
Input: head = [7,7,7,7], val = 7
Output: []


My Code:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* removeElements(struct ListNode* head, int val){
    struct ListNode* Head = malloc(sizeof(struct ListNode));
    Head->next = head;
    struct ListNode* temp = Head;

    while (temp->next != NULL)
    {
        if (temp->next->val != val)
        {temp = temp->next;}
        else
        {
            temp->next = temp->next->next;
        }
    }
    return Head->next;
}
Time: 8 ms                      beat:97.16%
Memory: 7.7MB              beat:92.71%



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 23:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表