两个非降序链表的并集,例如将链表1->2->3 和 2->3->5 并为 1->2->3->5,只能输出结果,不能修改两个链表的数据。
时间: 2023-03-11 15:06:36 浏览: 189
按照题目要求,可以使用双指针法来解决该问题。首先,定义两个指针分别指向两个链表的头结点,比较两个指针的值,较小的节点添加到新的链表中,然后将指针指向下一个节点,直到其中一个链表遍历完,将另一个链表剩余的节点添加到新的链表的末尾,从而得到两个非降序链表的并集。
相关问题
用C语言写两个非降序链表的并集,例如将链表1->2->3 和 2->3->5 并为 1->2->3->5,只能输出结果,不能修改两个链表的数据。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int val;
struct node *next;
} Node;
Node *merge(Node *l1, Node *l2) {
Node *dummy = (Node *)malloc(sizeof(Node));
Node *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;
}
cur->next = l1 ? l1 : l2;
return dummy->next;
}
int main() {
Node *l1 = (Node *)malloc(sizeof(Node));
l1->val = 1;
l1->next = (Node *)malloc(sizeof(Node));
l1->next->val = 2;
l1->next->next = (Node *)malloc(sizeof(Node));
l1->next->next->val = 3;
l1->next->next->next = NULL;
Node *l2 = (Node *)malloc(sizeof(Node));
l2->val = 2;
l2->next = (Node *)malloc(sizeof(Node));
l2->next->val = 3;
l2->next->next = (Node *)malloc(sizeof(Node));
l2->next->next->val = 5;
l2->next->next->next = NULL;
Node *result = merge(l1, l2);
while (result) {
printf("%d ", result->val);
result = result->next;
}
printf("\n");
return 0;
}
```
用c语言写程序,【问题描述】 两个非降序链表的并集,例如将链表1->2->3 和 2->3->5 并为 1->2->3->5,只能输出结果,不能修改两个链表的数据。 【输入形式】 第一行为第一个链表的各结点值,以空格分隔。 第二行为第二个链表的各结点值,以空格分隔。 【输出形式】 合并好的链表,以非降序排列,值与值之间以空格分隔。 【样例输入】 4 7 10 34 1 4 6 29 34 34 52 【样例输出】 1 4 6 7 10 29 34 52 【评分标准】 要使用链表实现,否则不能得分。
以下是用C语言实现的代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表结构体
typedef struct Node {
int val;
struct Node* next;
} Node;
// 创建链表的函数
Node* createList() {
Node* head = NULL;
Node* tail = NULL;
int val;
while (scanf("%d", &val) == 1) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->val = val;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
}
return head;
}
// 合并两个链表的函数
Node* mergeLists(Node* l1, Node* l2) {
Node* head = NULL;
Node* tail = NULL;
while (l1 != NULL && l2 != NULL) {
if (l1->val <= l2->val) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->val = l1->val;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
l1 = l1->next;
} else {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->val = l2->val;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
l2 = l2->next;
}
}
while (l1 != NULL) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->val = l1->val;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
l1 = l1->next;
}
while (l2 != NULL) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->val = l2->val;
newNode->next = NULL;
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
tail = newNode;
}
l2 = l2->next;
}
return head;
}
// 输出链表的函数
void printList(Node* head) {
Node* p = head;
while (p != NULL) {
printf("%d ", p->val);
p = p->next;
}
printf("\n");
}
int main() {
// 创建两个链表
Node* l1 = createList();
Node* l2 = createList();
// 合并两个链表
Node* mergedList = mergeLists(l1, l2);
// 输出合并后的链表
printList(mergedList);
return 0;
}
```
输入样例:
```
4 7 10 34
1 4 6 29 34 34 52
```
输出样例:
```
1 4 6 7 10 29 34 52
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)