ListNode * MergeOrderList(ListNode *list1, ListNode *list2) { //List1是链表一的第一个节点 ListNode *cur1 = list1; ListNode *cur2 = list2; ListNode *result = NULL; ListNode *tail = NULL; ListNode *next; while(cur1 != NULL && cur2 != NULL){ //取短的链表 if(cur1->data <= c
时间: 2023-11-16 22:06:14 浏览: 82
这段代码似乎不完整,缺失了一部分。但是从代码的基本结构来看,这是一个将两个有序链表合并成一个有序链表的算法。
具体来说,算法的主要思路是:定义两个指针cur1和cur2分别指向两个有序链表list1和list2的头节点,然后比较cur1和cur2指向的节点的大小,将较小的节点连接到结果链表result的尾部,并将结果链表的尾部tail指向新加入的节点。不断重复这个过程,直到某一个链表遍历完为止。最后,将未遍历完的链表剩余的节点直接连接到结果链表的尾部即可。
需要注意的是,该算法的时间复杂度为O(m+n),其中m和n分别为两个链表的长度。
相关问题
//合并两个单链表,合并后依然有序(升序) ListNode * MergeOrderList(ListNode *list1, ListNode *list2) { //List1是链表一的第一个节点 ListNode *cur1 = list1; ListNode *cur2 = list2; ListNode *result = NULL; ListNode *tail = NULL; ListNode *next; while(cur1 != NULL && cur2 != NULL){ //取短
的链表的节点插入到结果链表中 if(cur1->val < cur2->val){ next = cur1->next; if(result == NULL){ result = cur1; tail = cur1; } else{ tail->next = cur1; tail = cur1; } cur1 = next; } else{ next = cur2->next; if(result == NULL){ result = cur2; tail = cur2; } else{ tail->next = cur2; tail = cur2; } cur2 = next; } } //将剩余的节点插入到结果链表中 if(cur1 != NULL){ if(result == NULL){ result = cur1; } else{ tail->next = cur1; } } if(cur2 != NULL){ if(result == NULL){ result = cur2; } else{ tail->next = cur2; } } return result; }
用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;}
以下是用 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;
}
阅读全文