首页 > 其他分享 >[Leetcode]合并两个有序链表

[Leetcode]合并两个有序链表

时间:2023-04-19 22:39:26浏览次数:40  
标签:ListNode struct cur1 next 链表 tail 有序 cur2 Leetcode

力扣链接

[Leetcode]合并两个有序链表_链表  合并

依次比较,取小的尾插:








初步代码:

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


struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    struct ListNode* head = NULL,*tail = NULL;
    struct ListNode* cur1 = list1;
    struct ListNode* cur2 = list2;
    while(cur1 && cur2)
    {
        if(cur1->val<cur2->val)
        {
            if(head == NULL)
            {
                head = tail = cur1;//为空直接赋值
            }
            else
            {
                 tail->next = cur1;
                 tail = tail->next;

            }
           
            cur1 = cur1->next;
            
        }
        else
        {
            if(head == NULL)
            {
                head = tail = cur2;
            }
            else
            {
                 tail->next = cur2;
                 tail = tail->next;

            }
           
            cur2 = cur2->next;

        }
    
    }
    if(cur2)
    {
        tail->next = cur2;
    }
    if(cur1)
    {
        tail->next = cur1;
    }
    return head;


}

[Leetcode]合并两个有序链表_链表  合并_02

通过测试用例来看,当其中一个链表为空时,上面的代码不执行while循环,直接执行下面的if语句,而此时tail为空,所以出现了错误.

修改方法

1.直接判空返回(在声明前加入)

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    if(list1 == NULL)//判空返回
         return list2;//
    if(list2 == NULL)//
         return list1;//

    struct ListNode* head = NULL,*tail = NULL;
    struct ListNode* cur1 = list1;
    struct ListNode* cur2 = list2;
    while(cur1 && cur2)
    {
        if(cur1->val<cur2->val)
        {
            if(head == NULL)
            {
                head = tail = cur1;//为空直接赋值
            }
            else
            {
                 tail->next = cur1;
                 tail = tail->next;


            }
           
            cur1 = cur1->next;
            
        }
        else
        {
            if(head == NULL)
            {
                head = tail = cur2;
            }
            else
            {
                 tail->next = cur2;
                 tail = tail->next;


            }
           
            cur2 = cur2->next;


        }
    
    }
    if(cur2)
    {
        tail->next = cur2;
    }
    if(cur1)
    {
        tail->next = cur1;
    }
    return head;



}

2.引用带哨兵卫的头结点(可以减少代码量,不存放有效数据)

哨兵位的头结点不存放有效数据,但是一直存在,所以不需要判空,需要开辟和释放空间

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



struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    struct ListNode* cur1 = list1;
    struct ListNode* cur2 = list2;
     struct ListNode* guard = NULL,*tail = NULL;
    guard = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
    tail->next = NULL;
    while(cur1 && cur2)
    {
        if(cur1->val<cur2->val)
        {
        //     if(head == NULL)
        //     {
                // head = tail = cur1;//为空直接赋值
            // }
            // else
            // {
                 tail->next = cur1;
                 tail = tail->next;
            // }          
                 cur1 = cur1->next;           
        }
        else
        {
            // if(head == NULL)
            // {
                // head = tail = cur2;
            // }
            // else
            // {
                 tail->next = cur2;
                 tail = tail->next;
            // }         
                cur2 = cur2->next;
        }
    
    }
    if(cur2)
    {
        tail->next = cur2;
    }
    if(cur1)
    {
        tail->next = cur1;
    }

    struct ListNode* head = guard->next;//存放哨兵位头结点的下一位,方便返回
    free(guard);
    return head;
}


标签:ListNode,struct,cur1,next,链表,tail,有序,cur2,Leetcode
From: https://blog.51cto.com/u_15992140/6207510

相关文章

  • #yyds干货盘点# LeetCode面试题:搜索旋转排序数组 II
    1.简述:已知存在一个按非降序排列的整数数组nums,数组中的值不必互不相同。在传递给函数之前,nums在预先未知的某个下标k(0<=k<nums.length)上进行了旋转,使数组变为[nums[k],nums[k+1],...,nums[n-1],nums[0],nums[1],...,nums[k-1]](下标从0开始计数)。例如,[0,1,2......
  • JZ18 删除链表的节点
    importjava.util.*;/**publicclassListNode{*intval;*ListNodenext=null;*publicListNode(intval){*this.val=val;*}*}*/publicclassSolution{/***代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法......
  • #yyds干货盘点# LeetCode程序员面试金典:串联所有单词的子串
    题目:给定一个字符串 s 和一个字符串数组 words。 words 中所有字符串长度相同。 s 中的串联子串是指一个包含 words 中所有字符串以任意顺序排列连接起来的子串。例如,如果 words=["ab","cd","ef"],那么 "abcdef", "abefcd","cdabef", "cdefab","efabcd",......
  • 35. 搜索插入位置(leetcode)
    https://leetcode.cn/problems/search-insert-position/简单二分,这里可以判断return,相当于剪枝这里的写法最后更新后的l或r一定可以使得nums[l]或者nums[r]>=target所以退出循环最后的l或r就是第一个大于等于target的下标classSolution{public:intsearchInsert(vect......
  • 704. 二分查找(leetcode)
    https://leetcode.cn/problems/binary-search/简单二分classSolution{public:intsearch(vector<int>&nums,inttarget){intl=0,r=nums.size()-1;while(l<r){intmid=l+r>>1;if(nums[mid]......
  • 递归-leetcode 114
    给你二叉树的根结点 root ,请你将它展开为一个单链表:展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null 。展开后的单链表应该与二叉树 先序遍历 顺序相同。示例1:输入:root=[1,2,5,3,4,null,6]输出:[1,null,2,null,3,nu......
  • 【DP】LeetCode 题号.题目
    题目链接377.组合总和Ⅳ思路分析动态规划题目的时候只需要考虑最后一个阶段,因为所有的阶段转化都是相同的,考虑最后一个阶段容易发现规律在数组的动态规划问题中,一般dp[i]都是表示以nums[i]为结尾的状态;dp[i][j]分别表示以nums1[i]和nums2[j]为结尾的状态,以此类推......
  • leetcode刷题随笔(2)
    42.收集雨水(TrappingRainWater)方法一:利用双指针交叉循环求解,时间复杂度O(n)//接雨水inttrap(vector<int>&height){inti=0,j=height.size()-1;intleft_max=0,right_max=0;intvan=0;while(i<j){if(height[i]......
  • LeetCode Top100: 反转链表 (python)
     给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。 示例1:输入:head=[1,2,3,4,5]输出:[5,4,3,2,1]示例2:输入:head=[1,2]输出:[2,1]示例3:输入:head=[]输出:[] 提示:链表中节点的数目范围是 [0,5000]-5000<=Node.val<=5000实现:给你......
  • LeetCode Top100: 翻转二叉树(python)
    给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。 示例1:输入:root=[4,2,7,1,3,6,9]输出:[4,7,2,9,6,3,1]示例2:输入:root=[2,1,3]输出:[2,3,1]示例3:输入:root=[]输出:[] 提示:树中节点数目范围在 [0,100] 内-100<=Node.val<=100实......