From e7f772afb4425f65d6821667ba78aa3edb52f2fc Mon Sep 17 00:00:00 2001 From: fixme Date: Wed, 28 Jul 2021 22:49:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0c=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0203.移除链表元素.md | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md index cac9f233..ea1d705a 100644 --- a/problems/0203.移除链表元素.md +++ b/problems/0203.移除链表元素.md @@ -146,8 +146,39 @@ public: ## 其他语言版本 +C: +```c +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ +struct ListNode* removeElements(struct ListNode* head, int val){ + typedef struct ListNode ListNode; + ListNode *shead; + shead = (ListNode *)malloc(sizeof(ListNode)); + shead->next = head; + ListNode *cur = shead; + while(cur->next != NULL){ + if (cur->next->val == val){ + ListNode *tmp = cur->next; + cur->next = cur->next->next; + free(tmp); + } + else{ + cur = cur->next; + } + } + head = shead->next; + free(shead); + return head; +} +``` + Java: ```java /**