Skip to content

Commit c08f38a

Browse files
committed
"Delete Node in a Linked List"
1 parent 9afba8d commit c08f38a

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ You can build all the files using `make` (Use MinGW GCC and GNU Make on Windows)
2424

2525
| | Problem | Solution |
2626
| --- | ------------------------------------------------------------ | ------------------ |
27+
| 237 | [Delete Node in a Linked List] | [C](src/237.c) |
28+
| 236 | [Lowest Common Ancestor of a Binary Tree] | |
2729
| 235 | [Lowest Common Ancestor of a Binary Search Tree] | [C](src/235.c) |
2830
| 234 | [Palindrome Linked List] | [C](src/234.c) |
2931
| 233 | [Number of Digit One] | |
@@ -248,6 +250,8 @@ You can build all the files using `make` (Use MinGW GCC and GNU Make on Windows)
248250
[LeetCode algorithm problems]: https://leetcode.com/problemset/algorithms/
249251

250252

253+
[Delete Node in a Linked List]: https://leetcode.com/problems/delete-node-in-a-linked-list/
254+
[Lowest Common Ancestor of a Binary Tree]: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
251255
[Lowest Common Ancestor of a Binary Search Tree]: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
252256
[Palindrome Linked List]: https://leetcode.com/problems/palindrome-linked-list/
253257
[Number of Digit One]: https://leetcode.com/problems/number-of-digit-one/

src/237.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
struct ListNode {
5+
int val;
6+
struct ListNode *next;
7+
};
8+
9+
void deleteNode(struct ListNode* node) {
10+
if (node == NULL || node->next == NULL) return;
11+
12+
node->val = node->next->val;
13+
node->next = node->next->next;
14+
}
15+
16+
void printList(struct ListNode* head) {
17+
while (head) {
18+
printf("%d->", head->val);
19+
head = head->next;
20+
}
21+
printf("N\n");
22+
}
23+
24+
int main() {
25+
26+
struct ListNode *l1 = (struct ListNode *)calloc(3, sizeof(struct ListNode));
27+
28+
l1->val = 1;
29+
l1->next = l1 + 1;
30+
31+
l1->next->val = 2;
32+
l1->next->next = l1 + 2;
33+
34+
l1->next->next->val = 3;
35+
l1->next->next->next = NULL;
36+
37+
printList(l1);
38+
39+
/* delete 2 */
40+
deleteNode(l1->next);
41+
42+
printList(l1);
43+
44+
return 0;
45+
}

0 commit comments

Comments
 (0)