21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists【easy】
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 (l1 == NULL || l2 == NULL) {
return l1 ? l1 : l2;
} ListNode * dummy = new ListNode(INT_MIN);
ListNode * temp = dummy; while (l1 && l2) {
if (l1->val > l2->val) {
dummy->next = l2;
l2 = l2->next;
}
else {
dummy->next = l1;
l1 = l1->next;
} dummy = dummy->next;
} if (l1 || l2) {
dummy->next = l1 ? l1 : l2;
} return temp->next;
}
};
由于最后是弄到list1中,但是我们不知道list1还是list2的第一个元素关系,最后结果的list1中的头结点可能会改变,所以需要引入dummy节点。
解法二:
 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(l1, l2.next);
             return l2;
         }
 }
参考了@yangliguang 的代码
解法三:
 public class Solution {
     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
         if (l1 == null) return l2;
         if (l2 == null) return l1;
         ListNode handler;
         if(l1.val < l2.val) {
             handler = l1;
             handler.next = mergeTwoLists(l1.next, l2);
         } else {
             handler = l2;
             handler.next = mergeTwoLists(l1, l2.next);
         }
         return handler;
     }
 }
参考了@RunRunCode 的代码
解法二和解法三都是递归,还没有完全弄明白……
21. Merge Two Sorted Lists【easy】的更多相关文章
- Leetcode 21. Merge Two Sorted Lists(easy)
		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 [Difficulty: Easy]
		题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ... 
- 两个升序链表的合并 Merge Two Sorted Lists 【 leetcode】
		class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode *p; ListN ... 
- LeetCode:21. Merge Two Sorted Lists(Easy)
		1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ... 
- 88. Merge Sorted Array【easy】
		88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ... 
- 刷题21. Merge Two Sorted Lists
		一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ... 
- [Leetcode][Python]21: Merge Two Sorted Lists
		# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ... 
- 21. Merge Two Sorted Lists(合并2个有序链表)
		21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ... 
- 160. Intersection of Two Linked Lists【easy】
		160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ... 
随机推荐
- 【贪心】【线性基】bzoj2844 albus就是要第一个出场
			引用题解:http://blog.csdn.net/PoPoQQQ/article/details/39829237 注意评论区. #include<cstdio> using names ... 
- IDEA ULTIMATE 2019.1 注册码,亲测可用
			在 hosts 文件里加入如下的配置: 0.0.0.0 account.jetbrains.com 0.0.0.0 www.jetbrains.com # 2019.1得加这个 注册码: N757JE ... 
- URL Schemes(转载)
			URL Schemes 应用在 iOS 上已经很久了.对于使用者来说,在沙盒机制下的 iOS 中,如果想做到一定程度上的自动化就不可避免地要用到 URL Schemes.但因为 URL Schemes ... 
- jvm-监控指令-jstack
			格式: jstack [option] vmid 选项: -l 除了堆栈信息外,显示关于锁的附加信息. 作用: 生成虚拟机当前时刻的线程快照. 目的: 定位线程长时间停顿的原因,比如线程间死锁.死循环 ... 
- GNS3 思科模块 说明
			GNS3整合了如下的软件: Dynamips :一款可以让用户直接运行Cisco系统(IOS)的模拟器 Dynagen :是Dynamips的文字显示前端 Pemu : PIX防火墙设备模 ... 
- 采用Apache作为WebLogic Server集群的负载均衡器
			强烈建议不要使用WebLogic ClusterServlet作为Proxy进行生产环境的负载均衡, 那个是用来进行集群的功能测试的,Oracle的产品文挡也写得比较清楚. 如果采用软件的负载均衡,可 ... 
- Jigsaw 项目:Java 模块系统新手引导
			前言 随着 2017 年 10 月 Java 9 的发布,Java 能够使用模块系统了,但是中文互联网上的资料太少,许多关于 Java 模块系统的文章都只是介绍了模块系统的好处,或者给了一些毫无组织的 ... 
- UVA-10603-Fill(BFS+优先队列)
			There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not grea ... 
- http://jingyan.baidu.com/article/dca1fa6fa07000f1a44052f6.html
			http://jingyan.baidu.com/article/dca1fa6fa07000f1a44052f6.html 
- Hadoop之Hbase详解
			1.什么是Hbase HBASE是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统, hbase是列式的分布式数据库 1.2.HBASE优势: 1)线性扩展,随着数据量增多可以通过节点扩展进行支撑 ... 
