用c写合并两个链表的代码
时间: 2023-08-31 18:42:21 浏览: 72
### 回答1:
这是一个简单的示例,可以用来合并两个链表:struct Node *MergeLists(struct Node *list1, struct Node *list2) {
struct Node *head = NULL;
struct Node *temp = NULL;
if (list1 == NULL)
return list2;
if (list2 == NULL)
return list1;
if (list1->data <= list2->data) {
head = list1;
list1 = list1->next;
}
else {
head = list2;
list2 = list2->next;
}
temp = head;
while (list1 != NULL && list2 != NULL) {
if (list1->data <= list2->data) {
temp->next = list1;
list1 = list1->next;
}
else {
temp->next = list2;
list2 = list2->next;
}
temp = temp->next;
}
if (list1 == NULL)
temp->next = list2;
else
temp->next = list1;
return head;
}
### 回答2:
合并两个链表的过程可以通过递归实现,具体代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
// 链表节点的数据结构
typedef struct Node {
int data;
struct Node* next;
} Node;
// 创建一个新的节点
Node* createNode(int value) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("内存分配失败\n");
exit(1);
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
// 合并两个有序链表
Node* mergeLists(Node* l1, Node* l2) {
// 递归终止条件
if (l1 == NULL)
return l2;
if (l2 == NULL)
return l1;
// 递归调用
if (l1->data <= l2->data) {
l1->next = mergeLists(l1->next, l2);
return l1;
} else {
l2->next = mergeLists(l1, l2->next);
return l2;
}
}
// 打印链表
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
// 创建链表1: 1->2->3
Node* l1 = createNode(1);
l1->next = createNode(2);
l1->next->next = createNode(3);
// 创建链表2: 4->5->6
Node* l2 = createNode(4);
l2->next = createNode(5);
l2->next->next = createNode(6);
// 合并链表
Node* mergedList = mergeLists(l1, l2);
// 打印结果
printf("合并后的链表: ");
printList(mergedList);
// 释放内存
Node* temp;
while (mergedList != NULL) {
temp = mergedList;
mergedList = mergedList->next;
free(temp);
}
return 0;
}
```
以上代码使用了递归的方式来合并两个有序链表,先检查链表中较小的节点,将其指向后面的已合并好的链表,然后递归调用合并剩余的部分。最后打印输出合并后的链表,并释放内存。
### 回答3:
以下是用C语言编写的合并两个链表的代码:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点
struct ListNode {
int data;
struct ListNode* next;
};
// 合并两个链表的函数
struct ListNode* mergeLists(struct ListNode* list1, struct ListNode* list2) {
// 创建一个新的节点作为合并后的链表的头节点
struct ListNode* mergedList = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* current = mergedList;
mergedList->next = NULL;
// 遍历两个链表,比较节点的值,并将较小的节点插入合并后的链表中
while(list1 != NULL && list2 != NULL) {
if(list1->data < list2->data) {
current->next = list1;
list1 = list1->next;
} else {
current->next = list2;
list2 = list2->next;
}
current = current->next;
}
// 把剩余节点添加到合并后的链表尾部
if(list1 == NULL) {
current->next = list2;
} else {
current->next = list1;
}
// 返回合并后的链表的头节点
return mergedList->next;
}
// 测试代码
int main() {
// 创建两个链表
struct ListNode* list1 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* list2 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* temp1 = list1;
struct ListNode* temp2 = list2;
// 构建链表1:1 -> 3 -> 5 -> 7 -> NULL
temp1->data = 1;
temp1->next = (struct ListNode*)malloc(sizeof(struct ListNode));
temp1 = temp1->next;
temp1->data = 3;
temp1->next = (struct ListNode*)malloc(sizeof(struct ListNode));
temp1 = temp1->next;
temp1->data = 5;
temp1->next = (struct ListNode*)malloc(sizeof(struct ListNode));
temp1 = temp1->next;
temp1->data = 7;
temp1->next = NULL;
// 构建链表2:2 -> 4 -> 6 -> 8 -> NULL
temp2->data = 2;
temp2->next = (struct ListNode*)malloc(sizeof(struct ListNode));
temp2 = temp2->next;
temp2->data = 4;
temp2->next = (struct ListNode*)malloc(sizeof(struct ListNode));
temp2 = temp2->next;
temp2->data = 6;
temp2->next = (struct ListNode*)malloc(sizeof(struct ListNode));
temp2 = temp2->next;
temp2->data = 8;
temp2->next = NULL;
// 合并两个链表
struct ListNode* mergedList = mergeLists(list1, list2);
// 打印合并后的链表
struct ListNode* current = mergedList;
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
return 0;
}
```
这段代码实现了合并两个链表的逻辑,将链表1和链表2的节点按照从小到大的顺序合并到一个新的链表中,并返回新链表的头节点。这个例子中,链表1包含节点1、3、5和7,链表2包含节点2、4、6和8,合并后的链表为1、2、3、4、5、6、7和8。