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 = l1while 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.nextif p1 == nil {q1.next = l2r = l1}}while let q2 = p2 {arr.append(q2.val)p2 = q2.nextif p2 == nil {q2.next = l1r = l2}}p1 = rarr.forEach {p1?.val = $0p1 = p1?.next}return r
}

[swift] LeetCode 21. Merge Two Sorted Lists相关推荐

  1. Leetcode 21:Merge Two Sorted Lists(golang实现合并两条已经排序的链表)

    21.Merge Two Sorted Lists 题目链接:题目链接 Merge two sorted linked lists and return it as a new list. The n ...

  2. LeetCode 21. Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  3. leetcode 21 Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  4. [LeetCode] 21. Merge Two Sorted Lists ☆

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  5. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  6. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public:ListNode* mergeTwoLists( ...

  7. 合并k个有序链表 python_[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  8. leetcode python3 简单题21. Merge Two Sorted Lists(Linked)

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十一题 (1)题目 英文: Merge two sorted linked li ...

  9. LeetCode 21. Merge Two Sorted Lists--合并2个有序列表--python递归,迭代解法

    题目地址: Merge two sorted linked lists and return it as a new list. The new list should be made by spli ...

最新文章

  1. ZooKeeper 源码和实践揭秘
  2. 【NLP傻瓜式教程】手把手带你RCNN文本分类(附代码)
  3. java文件客户端下载_使用Java写一个minio的客户端上传下载文件
  4. 13 PP配置-生产主数据-BOM相关-定义修正参数
  5. centos 6.5 安装mysql rpm_centos6.5 rpm 安装mysql5.6
  6. 打印服务器自动关闭,win7打印机服务print spooler老是自动关闭怎么解决
  7. php中文歌词,arash的中文歌词 - 波斯语 | Persian | فارسی - 声同小语种论坛 - Powered by phpwind...
  8. 【hiho】2018ICPC北京赛区网络赛B Tomb Raider(暴力dfs)
  9. 那些便宜的vps,你敢用吗?企业该如何选择云服务器?
  10. 2017H1日本畅销榜:《怪物弹珠》霸气连庄 FGO再掀二次元风暴
  11. 这份整理的图解Java(全彩版)火了,完整PDF开放下载
  12. android 输入法,里面还集成语音输入
  13. 《软件创富》 序:1997前后
  14. 某网页在线视频有声音无图像
  15. 学习笔记:SpringCloud 微服务技术栈_实用篇②_黑马旅游案例
  16. inurl_inurl,intitle指令的含义、作用及用法
  17. PromSQL v2.29
  18. char型和long型的转换
  19. 干货 | 什么是高频电解电容,它有普通电解电容有什么区别?
  20. 详解KPL商业生态,跑步前进实现多赢

热门文章

  1. C++ 中的Virtual Function (虚函数)
  2. textarea最大长度限制
  3. 转换jsonArray异常——由hibernate引起的转换jsonArray异常解决办法
  4. WPF、WCF、WF打造Hello World程序
  5. Ubuntu通过vnc连接Windows主机的问题解决
  6. 125条常见的java面试笔试题大汇总2
  7. 上:Spark VS Flink – 下一代大数据计算引擎之争,谁主沉浮?
  8. 自定义元素探秘及构建可复用组件最佳实践 1
  9. 浅析GitLab Flow的十一个规则
  10. Ojbect-C     NSArray和NSMutableArray数组的使用   有关API查询