首页 > 其他 > 详细

leetcode 合并两个有序链表 简单

时间:2021-07-22 23:40:08      阅读:31      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 

没什么好说的,很简单。也没有坑点。仔细一点就一发A

 

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode *head = new ListNode;
        auto tmp = head;
        while(l1 && l2) {
            if(l1 -> val < l2 -> val) {
                head -> next = l1;
                l1 = l1 -> next;
            }
            else {
                head -> next = l2;
                l2 = l2 -> next;
            }
            head = head -> next;
        }
        while(l1) {
            head -> next = l1;
            l1 = l1 -> next;
            head = head -> next;
        }
        while(l2) {
            head -> next = l2;
            l2 = l2 -> next;
            head = head -> next;
        }
        return tmp -> next;
    }
};

 

leetcode 合并两个有序链表 简单

原文:https://www.cnblogs.com/rookie-acmer/p/15046771.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!