Skip to content

Commit 9214930

Browse files
Merge pull request #525 from bdzyq/master
C实现
2 parents 22b783f + e7f772a commit 9214930

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

problems/0203.移除链表元素.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,39 @@ public:
146146
147147
148148
## 其他语言版本
149+
C:
150+
```c
151+
/**
152+
* Definition for singly-linked list.
153+
* struct ListNode {
154+
* int val;
155+
* struct ListNode *next;
156+
* };
157+
*/
149158
150159
160+
struct ListNode* removeElements(struct ListNode* head, int val){
161+
typedef struct ListNode ListNode;
162+
ListNode *shead;
163+
shead = (ListNode *)malloc(sizeof(ListNode));
164+
shead->next = head;
165+
ListNode *cur = shead;
166+
while(cur->next != NULL){
167+
if (cur->next->val == val){
168+
ListNode *tmp = cur->next;
169+
cur->next = cur->next->next;
170+
free(tmp);
171+
}
172+
else{
173+
cur = cur->next;
174+
}
175+
}
176+
head = shead->next;
177+
free(shead);
178+
return head;
179+
}
180+
```
181+
151182
Java:
152183
```java
153184
/**

0 commit comments

Comments
 (0)