[swift] LeetCode 21. Merge Two Sorted Lists

栏目: Swift · 发布时间: 7年前

内容简介:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.(随着网站访问量的激增,服务器配置只得一再升级以维持网站不“404 Not Found”,所以网站的维护费用也在不断上涨……(目前的阿里云服务器ECS+云数据库RDS+域名购买+七牛云的费用是2200元/年),为了能不放弃该网站

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

func mergeTwoLists(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
    var arr = [Int](), p1 = l1, p2 = l2, r = l1
 
    while let q1 = p1, let q2 = p2 {
        if q1.val < q2.val {
            arr.append(q1.val)
            p1 = q1.next
        } else {
            arr.append(q2.val)
            p2 = q2.next
        }
    }
    while let q1 = p1 {
        arr.append(q1.val)
        p1 = q1.next
        if p1 == nil {
            q1.next = l2
            r = l1
        }
    }
    while let q2 = p2 {
        arr.append(q2.val)
        p2 = q2.next
        if p2 == nil {
            q2.next = l1
            r = l2
        }
    }
 
    p1 = r
    arr.forEach {
        p1?.val = $0
        p1 = p1?.next
    }
 
    return r
}
[swift] LeetCode 21. Merge Two Sorted Lists [swift] LeetCode 21. Merge Two Sorted Lists

(随着网站访问量的激增,服务器配置只得一再升级以维持网站不“404 Not Found”,所以网站的维护费用也在不断上涨……(目前的阿里云服务器ECS+云数据库RDS+域名购买+七牛云的费用是2200元/年),为了能不放弃该网站,所以我又把打赏链接放上来啦~所有打赏金额都会被记账并投入博客维护中,感谢厚爱,多多关照~)


以上所述就是小编给大家介绍的《[swift] LeetCode 21. Merge Two Sorted Lists》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Effective Python

Effective Python

布雷特·斯拉特金(Brett Slatkin) / 爱飞翔 / 机械工业出版社 / 2016-1 / 59

用Python编写程序,是相当容易的,所以这门语言非常流行。但若想掌握Python所特有的优势、魅力和表达能力,则相当困难,而且语言中还有很多隐藏的陷阱,容易令开发者犯错。 本书可以帮你掌握真正的Pythonic编程方式,令你能够完全发挥出Python语言的强大功能,并写出健壮而高效的代码。Scott Meyers在畅销书《Effective C++》中开创了一种以使用场景为主导的精练教学方......一起来看看 《Effective Python》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具