学习时间:
2023年1月21日
题目描述:
![](https://img-blog.csdnimg.cn/b1ebf428cb054fdf9f03b05a4ce72f90.png)
题解分享:
// 作 者 : 繁 华 倾 夏
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h> // 调用malloc函数
// 力扣(LeetCode):21. 合并两个有序链表
//Definition for singly-linked list. 单向链表的定义
struct ListNode {
int val;
struct ListNode *next;
};
// 方法一:新建一个不带头节点的链表
// list1:链表 list2:链表
struct ListNode* mergeTwoLists1(struct ListNode* list1, struct ListNode* list2) {
struct ListNode* l1 = list1;
struct ListNode* l2 = list2;
// 如果其中一个链表为空,则返回另一个
if (l1 == NULL) {
return l2;
}
if (l2 == NULL) {
return l1;
}
struct ListNode* head = NULL, * tail = NULL; // 新链表的头节点和尾结点
// 先取一个小的val作为头节点
if (l1->val < l2->val) {
head = tail = l1; // 头节点和尾结点指向最小的节点
l1 = l1->next;
}
else {
head = tail = l2; // 头节点和尾结点指向最小的节点
l2 = l2->next;
}
while (l1 && l2) { // 遍历l1和l2链表
if (l1->val < l2->val) { // 如果小于则证明l1的val需要插入新链表
tail->next = l1; // 新链表的尾指针tail的next指向l1,代表next中的存储地址变为l1的首地址
tail = l1; // tail指向l1元素,代表移动指针指向
l1 = l1->next; // 移动l1指向下一个元素继续进行比较
}
else {
tail->next = l2; // 新链表的尾指针tail的next指向l2,代表next中的存储地址变为l2的首地址
tail = l2; // tail指向l2元素,代表移动指针指向
l2 = l2->next; // 移动l2指向下一个元素继续进行比较
}
}
if (l1) { // 还需要判断l1或l2数据是否都转移到新链表中
tail->next = l1; // 如果l1或l2链表还有数据的话
} // 则需要将l1或l2的数据链接到新链表上
if (l2) { // 因为while循环退出的条件为l1或l2为空
tail->next = l2; // 则l1或l2两个都为空,或者两者有一个为空
} // 不为空的链表上的数据,则需要链接到新链表上
return head; // 返回头节点
}
// 方法二:新建一个带头节点的链表
// list1:链表 list2:链表
struct ListNode* mergeTwoLists2(struct ListNode* list1, struct ListNode* list2) {
struct ListNode* l1 = list1;
struct ListNode* l2 = list2;
// 如果其中一个链表为空,则返回另一个
if (l1 == NULL) {
return l2;
}
if (l2 == NULL) {
return l1;
}
struct ListNode* head = NULL, * tail = NULL; // 新链表的头节点和尾结点
head = tail = (struct ListNode*)malloc(sizeof(struct ListNode)); // 给带头节点的链表申请初始空间
while (l1 && l2) { // 遍历l1和l2链表
if (l1->val < l2->val) { // 如果小于则证明l1的val需要插入新链表
tail->next = l1; // 新链表的尾指针tail的next指向l1,代表next中的存储地址变为l1的首地址
tail = l1; // tail指向l1元素,代表移动指针指向
l1 = l1->next; // 移动l1指向下一个元素继续进行比较
}
else {
tail->next = l2; // 新链表的尾指针tail的next指向l2,代表next中的存储地址变为l2的首地址
tail = l2; // tail指向l2元素,代表移动指针指向
l2 = l2->next; // 移动l2指向下一个元素继续进行比较
}
}
if (l1) { // 还需要判断l1或l2数据是否都转移到新链表中
tail->next = l1; // 如果l1或l2链表还有数据的话
} // 则需要将l1或l2的数据链接到新链表上
if (l2) { // 因为while循环退出的条件为l1或l2为空
tail->next = l2; // 则l1或l2两个都为空,或者两者有一个为空
} // 不为空的链表上的数据,则需要链接到新链表上
struct ListNode* list = head->next; // 新建一个指针指向第一个位置元素,目的是去除哨兵位头节点
free(head); // 申请空间后需要free掉
return list; // 此时返回了不带头节点的链表
}
// 从尾部插入数据
void SListPushBack(struct ListNode** pphead, int x) {
struct ListNode* newnode = (struct ListNode*)malloc(sizeof(struct ListNode));
newnode->val = x;
newnode->next = NULL;
if (*pphead == NULL) {
*pphead = newnode;
}
else {
// 找到尾节点
struct ListNode* tail = *pphead;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = newnode;
}
}
// 打印链表
void SListPrint(struct ListNode* phead)
{
struct ListNode* cur = phead;
while (cur != NULL) {
printf("%d->", cur->val);
cur = cur->next;
}
printf("NULL\n");
}
// 测试用例
// 输入 l1 = [1, 2, 4], l2 = [1, 3, 4]
// 输出 [1, 1, 2, 3, 4, 4]
int main() {
struct ListNode* list1 = NULL; // 建立空链表
struct ListNode* list2 = NULL;
SListPushBack(&list1, 1); // 为链表插入数据
SListPushBack(&list1, 2);
SListPushBack(&list1, 4);
SListPushBack(&list2, 1);
SListPushBack(&list2, 3);
SListPushBack(&list2, 4);
struct ListNode* head = mergeTwoLists1(list1, list2);
//struct ListNode* head = mergeTwoLists2(list1, list2);
SListPrint(head); // 打印链表
return 0;
}
【繁华倾夏】【每日力扣题解分享】【Day7】