给我写一个由两个链表合成一个链表的代码
时间: 2024-05-06 07:21:27 浏览: 18
好的,以下是由两个链表合成一个链表的 Python 代码实现:
```python
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def mergeTwoLists(l1: ListNode, l2: ListNode) -> ListNode:
dummy = ListNode(0)
cur = dummy
while l1 and 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
```
希望可以帮到你。如果您还有其他问题,请随时问我。
相关问题
给我用c语言写一个由两个链表合成一个链表的代码
当然,我很乐意帮您解决这个问题。以下是一个用C语言编写的合并两个链表的代码:
```
#include <stdio.h>
#include <stdlib.h>
//链表节点的结构体
struct ListNode {
int val;
struct ListNode *next;
};
//合并两个链表的函数
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
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;
}
cur->next = l1 ? l1 : l2;
return dummy->next;
}
//主函数
int main() {
//创建第一个链表
struct ListNode* l1 = (struct ListNode*)malloc(sizeof(struct ListNode));
l1->val = 1;
l1->next = (struct ListNode*)malloc(sizeof(struct ListNode));
l1->next->val = 2;
l1->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
l1->next->next->val = 4;
l1->next->next->next = NULL;
//创建第二个链表
struct ListNode* l2 = (struct ListNode*)malloc(sizeof(struct ListNode));
l2->val = 1;
l2->next = (struct ListNode*)malloc(sizeof(struct ListNode));
l2->next->val = 3;
l2->next->next = (struct ListNode*)malloc(sizeof(struct ListNode));
l2->next->next->val = 4;
l2->next->next->next = NULL;
//合并两个链表
struct ListNode* merged = mergeTwoLists(l1, l2);
//输出结果
while (merged) {
printf("%d ", merged->val);
merged = merged->next;
}
return 0;
}
```
这个代码可以将两个链表合并成一个升序排列的新链表。
用c语言实现将两个非递减链表合成一个递减链表
可以按照以下步骤实现:
1. 定义两个链表的指针,分别指向两个非递减链表的头结点。
2. 定义一个新链表的指针,初始值为 NULL。
3. 循环比较两个链表的头结点的值,将较小的值插入到新链表的头部,并将指向该节点的指针后移。
4. 重复步骤 3 直到两个链表中有一个为空。
5. 将剩余的链表连接到新链表的尾部。
6. 返回新链表的头指针。
以下是示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
struct node {
int val;
struct node* next;
};
struct node* merge_lists(struct node* l1, struct node* l2) {
struct node* new_head = NULL;
struct node** p = &new_head;
while (l1 != NULL && l2 != NULL) {
if (l1->val <= l2->val) {
*p = l1;
l1 = l1->next;
} else {
*p = l2;
l2 = l2->next;
}
p = &((*p)->next);
}
if (l1 != NULL) {
*p = l1;
} else {
*p = l2;
}
struct node* prev = NULL;
struct node* curr = new_head;
while (curr != NULL) {
struct node* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
int main() {
struct node* l1 = (struct node*)malloc(sizeof(struct node));
struct node* l2 = (struct node*)malloc(sizeof(struct node));
struct node* p1 = l1;
struct node* p2 = l2;
// 构造两个非递减链表
for (int i = 1; i <= 5; i++) {
p1->val = i;
p1->next = (struct node*)malloc(sizeof(struct node));
p1 = p1->next;
p2->val = i + 5;
p2->next = (struct node*)malloc(sizeof(struct node));
p2 = p2->next;
}
p1->next = NULL;
p2->next = NULL;
struct node* merged_list = merge_lists(l1, l2);
// 输出合并后的递减链表
while (merged_list != NULL) {
printf("%d ", merged_list->val);
merged_list = merged_list->next;
}
return 0;
}
```