数据结构--链表题三--合并两个有序链表(leetcode-21)

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例 1:
在这里插入图片描述
输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]

示例 2:
输入:l1 = [], l2 = []
输出:[]

示例 3:
输入:l1 = [], l2 = [0]
输出:[0]

提示:
两个链表的节点数目范围是 [0, 50]
-100 <= Node.val <= 100
l1 和 l2 均按 非递减顺序 排列

题解:
由于没有看清题(没看到两链表都是非降序排列),解法有点麻烦,后面一种方法运用递归来求解,代码行数大减,解决问题前还是得先思考思考有没有什么便捷的方法,不能一上来就暴力求解

思路1如下:
1.考虑特殊情况,有一个链表为空,则返回另一个链表即可
2.一般情况:计算节点数,并将两个链表的值都放到一个数组里面
3.比较大的那一个链表拿来重新装值(设置一个慢指针,指向上一个节点,最后退出while时,令该节点等于慢指针所在的节点),接着新开节点(这里设置了一个flag标志是不是第一次开节点,如果是第一次,那么就要将),最后记得另最后开的一个节点指向NULL(记得开一个根节点指向表头)

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    struct ListNode* cont1 = list1;
    struct ListNode* cont2 = list2;
    struct ListNode* head;
    struct ListNode* fwd;
    struct ListNode* newpt;
    char length1 = 0;
    char length2 = 0;
    char length3 = 0;
    char temp;
    char num[101] = {0};
    char flag = 0;
    
    /*
    **计算节点数
    */
    if(list1 == NULL)
        return list2;
    if(list2 == NULL)
        return list1;
    while(cont1){
        num[length1] = cont1->val;
        length1++;
        cont1 = cont1->next;
    }
    while(cont2){
        num[length1 + length2] = cont2->val;
        length2++;
        cont2 = cont2->next;
    }
    length3 = length1 + length2 ;

    /*
    **对num排序
    */
    for(char i = 0; i < length3 - 1; i++)
        for(char j = i + 1; j < length3; j++){
            if(num[i] > num[j]){
                temp = num[i];
                num[i] = num[j];
                num[j] = temp;
            }
        }

    /*
    **为新节点配置内存
    */
    if(length1 >= length2){
        head = list1;
        for(char i = 0; i < length1 ; i++){
            list1->val = num[i];
            fwd = list1;
            list1 = list1->next;
        }
        list1 = fwd;
        for(char i = length1 ; i < length3; i++){
            if(flag == 0){
                newpt = (struct ListNode*)malloc( sizeof(struct ListNode));
                list1->next = newpt;
                flag = 1;
            }
            else{
                newpt->next = (struct ListNode*)malloc( sizeof(struct ListNode));
                newpt = newpt->next;
            }
            newpt->val = num[i];
        }
        newpt->next = NULL;
    }
    else{
        head = list2;
        for(char i = 0; i < length2; i++){
            list2->val = num[i];
            fwd = list2;
            list2 = list2->next;
        }
        list2 = fwd;
        for(char i = length2 ; i < length3; i++){
            if(flag == 0){
                newpt = (struct ListNode*)malloc( sizeof(struct ListNode));
                list2->next = newpt;
                flag = 1;
            }
            else{
                newpt->next = (struct ListNode*)malloc( sizeof(struct ListNode));
                newpt = newpt->next;
            }
            newpt->val = num[i];
        }
        newpt->next = NULL;
    }
    return head;
}


思路2如下:
运用递归思想,因为两个链表都是有序的,只要处理到有一个为空,则只需要将其最后一个节点指向另一个链表待处理的那个节点,再返回最先为空的链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    if(list1 == NULL)
        return list2;
    if(list2 == NULL)
        return list1;

    /*
    **list1和list2的值比较大小,运用递归,只要有一个为空,则返回先没了的即可
    */
    if(list1->val <= list2->val){  
        list1->next = mergeTwoLists(list1->next, list2);
        return list1;
    }
    else{
        list2->next = mergeTwoLists(list1, list2->next);
        return list2;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要求合并两个有序链表。对于链表中的每一个节点,由于链表有序的,所以可以将两个链表中的节点按照大小顺序进行比较,然后逐个将较小的节点链接上去,最终得到一个新的有序链表。 使用Rust语言实现这个问,需要首先定义一个链表节点的结构体,包含node值以及next指针。然后定义一个函数来合并两个有序链表输入两个链表的头节点指针,输出为新的有序链表的头节点指针。 在合并过程中,首先需要判断两个链表的头节点哪一个较小,将较小的节点作为新链表的头节点,并将该节点的next指针指向递归调用合并函数的结果。递归结束条件为其中一个链表为空,则将另一个链表直接链接到新链表上。 完整代码如下: ```rust // 定义链表节点结构体 #[derive(Debug)] struct ListNode { val: i32, next: Option<Box<ListNode>>, } impl ListNode { fn new(val: i32) -> Self { ListNode { val, next: None } } } // 合并两个有序链表 fn merge_two_lists(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> { match (l1, l2) { (None, None) => None, // 两个链表均为空 (l1, None) => l1, // 其中一个链表为空,直接返回另一个链表 (None, l2) => l2, (Some(mut l1), Some(mut l2)) => { if l1.val < l2.val { l1.next = merge_two_lists(l1.next, Some(l2)); Some(l1) } else { l2.next = merge_two_lists(Some(l1), l2.next); Some(l2) } } } } // 测试代码 fn main() { let l1 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 2, next: Some(Box::new(ListNode { val: 4, next: None, })), })), })); let l2 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 3, next: Some(Box::new(ListNode { val: 4, next: None, })), })), })); let merged = merge_two_lists(l1, l2); println!("{:?}", merged); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZSTU_呆大鹅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值