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 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相关推荐
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public:ListNode* mergeTwoLists( ...
- 合并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 ...
- leetcode python3 简单题21. Merge Two Sorted Lists(Linked)
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第二十一题 (1)题目 英文: Merge two sorted linked li ...
- 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 ...
最新文章
- 从GPT-3到DETR,一起来盘点2020有哪些突破?
- 科普:UTF-8 GBK UTF8 GB2312 之间的区别和关系
- Docker 的两类存储资源 - 每天5分钟玩转 Docker 容器技术(38)
- 使用WMI编程获取主机硬件信息(CPU_ID,硬盘、主板、BIOS序列号,Mac地址)
- 搭建SpringMVC
- Python3——通用序列操作
- k8s基本概念-如何使用私有regsitry
- 算法:(BFS)迷宫寻路算法
- C++入门基础知识总结(2022整理)
- 企业上云的选择及好处
- Linux工具篇 | Ubuntu安装string命令
- linux /dev/dsp: 没有那个文件或目录 解决方法
- 文件包含漏洞 文件伪协议利用
- Android10源码下载与编译(Mac移动硬盘)
- python pip 运用及下载
- CSDN博客炫丽图标调整字体大小和颜色
- 基于Ryu 防火墙的检测和解决异常入侵的流量--实验
- 医疗健康大数据: 应用实例与系统
- DCDC自举电容(BST电容)介绍
- [work]马尔可夫链 (Markov Chain)是什么鬼