用C语言改写,将打印输出的结果中重复的数去掉#include <stdio.h>#include <stdlib.h>struct ListNode { int val; struct ListNode *next;};struct ListNode* mergeList(struct ListNode* l1, struct ListNode* l2){ struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode)); // 新建一个虚拟头节点 dummy->val = 0; dummy->next = NULL; struct ListNode* cur = dummy; while (l1 && l2) { if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; } else { cur->next = l2; l2 = l2->next; } cur = cur->next; } if (l1) cur->next = l1; // 将剩余的节点加入到新链表中 if (l2) cur->next = l2; return dummy->next;}int main() { int n, m, num; scanf("%d%d", &n, &m); struct ListNode* l1 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* l2 = (struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* p1 = l1; struct ListNode* p2 = l2; for (int i = 0; i < n; i++) { scanf("%d", &num); p1->next = (struct ListNode*)malloc(sizeof(struct ListNode)); p1->next->val = num; p1->next->next = NULL; p1 = p1->next; } for (int i = 0; i < m; i++) { scanf("%d", &num); p2->next = (struct ListNode*)malloc(sizeof(struct ListNode)); p2->next->val = num; p2->next->next = NULL; p2 = p2->next; } struct ListNode* res = mergeList(l1->next, l2->next); while (res) { printf("%d ", res->val); res = res->next; } return 0;}
时间: 2024-04-04 20:31:31 浏览: 87
以下是用 C 语言改写,并去掉重复数的代码:
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* mergeList(struct ListNode* l1, struct ListNode* l2){
struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode)); // 新建一个虚拟头节点
dummy->val = 0;
dummy->next = NULL;
struct ListNode* cur = dummy;
while (l1 && l2) {
if (l1->val <= l2->val) {
cur->next = l1;
l1 = l1->next;
} else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
if (l1) cur->next = l1; // 将剩余的节点加入到新链表中
if (l2) cur->next = l2;
return dummy->next;
}
int main() {
int n, m, num;
scanf("%d%d", &n, &m);
struct ListNode* l1 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* l2 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* p1 = l1;
struct ListNode* p2 = l2;
for (int i = 0; i < n; i++) {
scanf("%d", &num);
p1->next = (struct ListNode*)malloc(sizeof(struct ListNode));
p1->next->val = num;
p1->next->next = NULL;
p1 = p1->next;
}
for (int i = 0; i < m; i++) {
scanf("%d", &num);
p2->next = (struct ListNode*)malloc(sizeof(struct ListNode));
p2->next->val = num;
p2->next->next = NULL;
p2 = p2->next;
}
struct ListNode* res = mergeList(l1->next, l2->next);
// 去除重复数
int hash[10001] = {0}; // 哈希表,假设节点值范围在 [0, 10000]
struct ListNode* cur = res;
struct ListNode* pre = NULL;
while (cur) {
if (hash[cur->val]) { // 如果当前节点值已经出现过,则删除当前节点
pre->next = cur->next;
free(cur);
cur = pre->next;
} else { // 如果当前节点值没有出现过,则将其加入哈希表,并继续遍历下一个节点
hash[cur->val] = 1;
pre = cur;
cur = cur->next;
}
}
// 输出结果
cur = res;
while (cur) {
printf("%d ", cur->val);
cur = cur->next;
}
// 释放内存
cur = res;
while (cur) {
struct ListNode* temp = cur;
cur = cur->next;
free(temp);
}
return 0;
}
阅读全文