鱼C论坛

 找回密码
 立即注册
查看: 2378|回复: 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:
  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. *     int val;
  5. *     struct ListNode *next;
  6. * };
  7. */


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

  12.     while (temp->next != NULL)
  13.     {
  14.         if (temp->next->val != val)
  15.         {temp = temp->next;}
  16.         else
  17.         {
  18.             temp->next = temp->next->next;
  19.         }
  20.     }
  21.     return Head->next;
  22. }
复制代码
Time: 8 ms                      beat:97.16%
Memory: 7.7MB              beat:92.71%



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 21:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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