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.

我的解决方式:

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/class Solution
{
public:ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {if(NULL==l1) return l2;if(NULL==l2) return l1;ListNode* head = NULL;if(l1->val < l2->val)     {head = l1; l1 = l1->next; }else                       { head = l2; l2 = l2->next;}ListNode* p = head;     // pointer to form new listwhile(l1!=NULL&&l2!=NULL){if(l1->val < l2->val){p->next = l1;l1 = l1 ->next;}else {p->next = l2;l2 = l2 ->next;}p = p->next;}if(l1){p->next = l1;}else{p->next = l2;}return head;}
};

递归c++解法:

class Solution {
public:ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {if(l1 == NULL) return l2;if(l2 == NULL) return l1;if(l1->val < l2->val) {l1->next = mergeTwoLists(l1->next, l2);return l1;} else {l2->next = mergeTwoLists(l2->next, l1);return l2;}}
};

python递归解决方式:

def mergeTwoLists(self, l1, l2):if not l1:return l2elif not l2:return l1else:if l1.val <= l2.val:l1.next = self.mergeTwoLists(l1.next, l2)return l1else:l2.next = self.mergeTwoLists(l1, l2.next)return l2

python非递归:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = Noneclass Solution:# @param {ListNode} l1# @param {ListNode} l2# @return {ListNode}def mergeTwoLists(self, l1, l2):p1 = l1p2 = l2guard = ListNode(0)q = guardwhile p1 is not None and p2 is not None:if p1.val <= p2.val:q.next = p1p1 = p1.nextq = q.nextelse:q.next = p2p2 = p2.nextq = q.nextif p1 is not None:q.next = p1if p2 is not None:q.next = p2return guard.next

python递归解决方式2:

If both lists are non-empty, I first make sure a starts smaller, use its head as result, and merge the remainders behind it. Otherwise, i.e., if one or both are empty, I just return what's there.class Solution:def mergeTwoLists(self, a, b):if a and b:if a.val > b.val:a, b = b, aa.next = self.mergeTwoLists(a.next, b)return a or b

转载于:https://www.cnblogs.com/mfrbuaa/p/5371604.html

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. [swift] 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. 从GPT-3到DETR,一起来盘点2020有哪些突破?
  2. 科普:UTF-8 GBK UTF8 GB2312 之间的区别和关系
  3. Docker 的两类存储资源 - 每天5分钟玩转 Docker 容器技术(38)
  4. 使用WMI编程获取主机硬件信息(CPU_ID,硬盘、主板、BIOS序列号,Mac地址)
  5. 搭建SpringMVC
  6. Python3——通用序列操作
  7. k8s基本概念-如何使用私有regsitry
  8. 算法:(BFS)迷宫寻路算法
  9. C++入门基础知识总结(2022整理)
  10. 企业上云的选择及好处
  11. Linux工具篇 | Ubuntu安装string命令
  12. linux /dev/dsp: 没有那个文件或目录 解决方法
  13. 文件包含漏洞 文件伪协议利用
  14. Android10源码下载与编译(Mac移动硬盘)
  15. python pip 运用及下载
  16. CSDN博客炫丽图标调整字体大小和颜色
  17. 基于Ryu 防火墙的检测和解决异常入侵的流量--实验
  18. 医疗健康大数据: 应用实例与系统
  19. DCDC自举电容(BST电容)介绍
  20. [work]马尔可夫链 (Markov Chain)是什么鬼

热门文章

  1. DOM中window的navigate()重新导航到制定网址
  2. Java-Runoob:Java Stream、File、IO
  3. logging模块的使用
  4. css3选择器的比较(二) -- 包含字符串
  5. python控制gpio的一段代码抄的
  6. Mini音乐播放器【简单版】(附源码)
  7. 【翻译】(5)Android.mk File
  8. PHP 5.3.1 安装包 VC9 VC6 区别是什么
  9. 在sql查询中使用表变量
  10. 常用的 css hack实例