Leetcode 21. 合并两个有序链表(DAY 83) ---- Leetcode Hot 100


原题题目


在这里插入图片描述


代码实现(首刷自解)


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(!l1) return l2;
        if(!l2) return l1;
        ListNode* ret = NULL,*pos = NULL;
        while(l1 && l2)
        {
            if(l1->val <= l2->val)
            {
                if(!ret)    pos = ret = l1;
                else
                {
                    pos->next = l1;
                    pos = pos->next;
                }
                l1 = l1->next;
            }
            else
            {
                if(!ret)    pos = ret = l2;
                else
                {
                    pos->next = l2;
                    pos = pos->next;
                }
                l2 = l2->next;
            }
        }
        if(l1) pos->next = l1;
        else    pos->next = l2;
        return ret;
    }
};

代码实现(二刷自解 DAY 136)


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(!l1) return l2;
        if(!l2) return l1;

        ListNode* ret = nullptr,*ptr = (l1->val <= l2->val) ? l1 : l2;
        if(l1->val <= l2->val)  l1 = l1->next;
        else    l2 = l2->next;
        if(!ret)    ret = ptr;
        while(l1 && l2)
        {
            if(l1->val <= l2->val)
            {
                ptr->next = l1;
                l1 = l1->next;
            }
            else
            {
                ptr->next = l2;
                l2 = l2->next;
            }
            ptr = ptr->next;
        }
        if(l1)  ptr->next = l1;
        else    ptr->next = l2;
        return ret;
    }
};

代码实现(三刷自解 DAY 195 C++)


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(!l1 || !l2)
        {
            if(!l1) return l2;
            if(!l2) return l1;
        }

        ListNode* ptr = nullptr,*tmp;
        while(l1 && l2)
        {
            if(l1->val <= l2->val)
            {
                if(!ptr)
                {
                    ptr = l1; 
                    tmp = l1;
                }
                else
                {
                    tmp->next = l1;
                    tmp = tmp->next;
                }
                l1 = l1->next;
            }
            else
            {
                if(!ptr)
                {
                    ptr = l2; 
                    tmp = l2;
                }
                else
                {
                    tmp->next = l2;
                    tmp = tmp->next;
                }
                l2 = l2->next;
            }
        }

        if(l1)  tmp->next = l1;
        if(l2)  tmp->next = l2;
        return ptr;
    }
};

代码实现(四刷自解 DAY 269 C++)


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* merge_sort(ListNode* list1, ListNode* list2)
    {
      if (!list1)   return list2;
      if (!list2)   return list1;

      ListNode* ptr1 = list1;
      ListNode* ptr2 = list2;
      ListNode* ret = nullptr,*ptr = nullptr;

      if(ptr1->val < ptr2->val)
      {
        ret = ptr1;
        ptr1 = ptr1->next;
      }
      else
      {
        ret = ptr2;
        ptr2 = ptr2->next;
      }

      ptr = ret;
      while(ptr1 && ptr2)
      {
        if (ptr1->val < ptr2->val)
        {
          ptr->next = ptr1;
          ptr1 = ptr1->next;
        }
        else
        {
          ptr->next = ptr2;
          ptr2 = ptr2->next;
        }
        ptr = ptr->next;
      }

      if(ptr1)  ptr->next = ptr1;
      if(ptr2)  ptr->next = ptr2;

      return ret;
    }

    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        ListNode* ret = merge_sort(list1, list2);

        return ret;
    }
};

代码实现(四刷自解 去重版本 DAY 269 C++)


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* merge_sort(ListNode* list1, ListNode* list2)
    {
      if (!list1)   return list2;
      if (!list2)   return list1;

      ListNode* ptr1 = list1;
      ListNode* ptr2 = list2;
      ListNode* ret = nullptr,*ptr = nullptr;

      if(ptr1->val < ptr2->val)
      {
        ret = ptr1;
        ptr1 = ptr1->next;
      }
      else
      {
        ret = ptr2;
        ptr2 = ptr2->next;
      }

      ptr = ret;
      while(ptr1 || ptr2)
      {
        ptr->next = nullptr;
        if (!ptr2 || ptr1 && ptr1->val < ptr2->val)
        { 
          if(ptr1->val != ptr->val)  ptr->next = ptr1;
          ptr1 = ptr1->next;
        }
        else
        {
          if(ptr2->val != ptr->val)  ptr->next = ptr2;
          ptr2 = ptr2->next;
        }
        if(ptr->next) ptr = ptr->next;
      }

      return ret;
    }

    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        ListNode* ret = merge_sort(list1, list2);

        return ret;
    }
};

代码实现(五刷自解 DAY 1 Golang)


/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode {
  var dummyhead ListNode

  now := &dummyhead
  for list1 != nil && list2 != nil {
    if list1.Val <= list2.Val {
      now.Next = list1
      list1 = list1.Next
    } else {
      now.Next = list2
      list2 = list2.Next
    }
    now = now.Next
  } 
  
  if list1 != nil {
    now.Next = list1
  }
  if list2 != nil {
    now.Next = list2
  }
  return dummyhead.Next
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Love 6

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

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

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

打赏作者

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

抵扣说明:

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

余额充值