-
Notifications
You must be signed in to change notification settings - Fork 0
/
removeListNode.c
78 lines (61 loc) · 1.63 KB
/
removeListNode.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode *createList() {
// dummy 简化编程,方便返回 head
struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode));
dummy->val = -1;
dummy->next = NULL;
struct ListNode *tail = dummy;
char ch;
while ((ch = getchar()) != '\n') {
if (ch == ' ')
continue; // 跳过空格字符
int num = ch - '0';
struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode));
node->val = num;
node->next = NULL;
tail->next = node;
tail = tail->next;
}
// todo: free()
return dummy->next;
}
struct ListNode *removeNode(struct ListNode *head, int num) {
// dummy 简化编程,方便删除 head
struct ListNode *dummy = (struct ListNode *)malloc(sizeof(struct ListNode));
dummy->val = -1;
dummy->next = head;
struct ListNode *node = dummy;
while (node->next->val != num) {
node = node->next;
}
struct ListNode *temp = node->next->next;
node->next->next = NULL;
free(node->next);
node->next = temp;
return dummy->next;
}
int main() {
struct ListNode *head = createList();
int num;
scanf_s("%d", &num);
struct ListNode *head2 = removeNode(head, num);
while (head2 != NULL) {
printf("%d ", head2->val);
head2 = head2->next;
}
return 0;
}
/*
联发科7.28软件开发笔试
题目很简单,就是让删除一个链表的某个节点,就是用 C 处理输入麻烦,用 C++ 用惯了,一些 C 技巧忘记了
输入格式:
1 2 3 4 5 6 // 链表的节点
3 // 待删除的数据
输出:
1 2 4 5 6
*/