c语言两个升序链表合并为升
时间: 2024-03-13 21:38:51 浏览: 26
以下是C语言将两个升序链表合并为一个新的升序链表的代码实现:
```c
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){
if (l1 == NULL) {
return l2;
}
if (l2 == NULL) {
return l1;
}
if (l1->val <= l2->val) {
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
```
相关问题
c语言实现两个升序链表合并为一个降序链表
下面是用 C 语言实现将两个升序链表合并为一个降序链表的示例代码:
```c
#include<stdio.h>
#include<stdlib.h>
struct ListNode {
int val;
struct ListNode* next;
};
struct ListNode* mergeLists(struct ListNode* l1, struct ListNode* l2) {
if (l1 == NULL) {
return l2;
}
if (l2 == NULL) {
return l1;
}
struct ListNode* merged_head;
if (l1->val < l2->val) {
merged_head = l1;
merged_head->next = mergeLists(l1->next, l2);
} else {
merged_head = l2;
merged_head->next = mergeLists(l1, l2->next);
}
return merged_head;
}
void printList(struct ListNode* head) {
struct ListNode* current = head;
while (current != NULL) {
printf("%d ", current->val);
current = current->next;
}
printf("\n");
}
int main() {
// 创建链表1:1 -> 3 -> 5
struct ListNode* l1 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* l1_1 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* l1_2 = (struct ListNode*)malloc(sizeof(struct ListNode));
l1->val = 1;
l1_1->val = 3;
l1_2->val = 5;
l1->next = l1_1;
l1_1->next = l1_2;
l1_2->next = NULL;
// 创建链表2:2 -> 4 -> 6
struct ListNode* l2 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* l2_1 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* l2_2 = (struct ListNode*)malloc(sizeof(struct ListNode));
l2->val = 2;
l2_1->val = 4;
l2_2->val = 6;
l2->next = l2_1;
l2_1->next = l2_2;
l2_2->next = NULL;
// 合并链表并打印结果
struct ListNode* merged = mergeLists(l1, l2);
printList(merged);
// 释放内存
free(l1);
free(l1_1);
free(l1_2);
free(l2);
free(l2_1);
free(l2_2);
return 0;
}
```
在这个示例中,我们首先定义了一个 `ListNode` 结构体来表示链表的节点。然后,实现了 `mergeLists` 函数来完成链表的合并。最后,在 `main` 函数中创建了两个升序链表,调用 `mergeLists` 函数进行合并,并打印结果。
请注意,在实际使用时,应该根据需要自行定义释放链表内存的函数,以避免内存泄漏。这里只是简单示例,没有包含完整的内存释放操作。
请用c语言实现对两个升序链表的合并
好的,以下是C语言实现对两个升序链表的合并的代码:
```c
#include <stdio.h>
#include <stdlib.h>
/* 定义链表节点结构体 */
struct Node {
int data;
struct Node* next;
};
/* 链表节点插入函数 */
void push(struct Node** head_ref, int new_data)
{
/* 为新的节点分配内存 */
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
/* 将数据赋值给新的节点 */
new_node->data = new_data;
/* 将新节点的下一个节点指向当前头结点 */
new_node->next = (*head_ref);
/* 将头结点指向新节点 */
(*head_ref) = new_node;
}
/* 合并两个升序链表的函数 */
struct Node* mergeLists(struct Node* a, struct Node* b)
{
struct Node* result = NULL;
/* 如果其中一个链表为空,则直接返回另一个链表 */
if (a == NULL)
return (b);
else if (b == NULL)
return (a);
/* 递归比较链表中的每个节点,将较小的节点插入到result链表中 */
if (a->data <= b->data) {
result = a;
result->next = mergeLists(a->next, b);
}
else {
result = b;
result->next = mergeLists(a, b->next);
}
return (result);
}
/* 输出链表的函数 */
void printList(struct Node* node)
{
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}
/* 主函数 */
int main()
{
struct Node* res = NULL;
/* 创建两个升序链表 */
struct Node* a = NULL;
struct Node* b = NULL;
push(&a, 15);
push(&a, 10);
push(&a, 5);
push(&b, 20);
push(&b, 3);
push(&b, 2);
/* 输出两个升序链表 */
printf("List 1: \n");
printList(a);
printf("\n");
printf("List 2: \n");
printList(b);
printf("\n");
/* 合并两个升序链表 */
res = mergeLists(a, b);
/* 输出合并后的链表 */
printf("Merged list: \n");
printList(res);
printf("\n");
return 0;
}
```
希望这个代码可以帮助解决你的问题。