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.
题目分析:本题是要合并两个已经有序的单链表,思路很简单,有两种方法:非递归和递归。
题目代码:
(1)非递归:
为方便操作,定义一个辅助的头节点,然后比较原来两个链表的头节点,将小的那一个加入到合并链表,最后,当其中一个链表为空时,直接将另一个链表接入到合并链表即可。
//public class LeetCode21 为测试
public class LeetCode21 {
public static void main(String[] args) {
ListNode m1=new ListNode(1),m2=new ListNode(3),m3=new ListNode(5);
m1.next=m2;
m2.next=m3;
System.out.println("链表1:"+m1.val+"->"+m2.val+"->"+m3.val);
ListNode n1=new ListNode(2),n2=new ListNode(4),n3=new ListNode(6);
n1.next=n2;
n2.next=n3;
System.out.println("链表2:"+n1.val+"->"+n2.val+"->"+n3.val);
ListNode result=new Solution().mergeTwoLists(m1, n1);
if(result!=null){
System.out.print("合并链表:"+result.val);
ListNode resultNext=result.next;
while(resultNext!=null){
System.out.print("->"+resultNext.val);
resultNext=resultNext.next;
}
}
}
}
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode fakeHead=new ListNode(0);
ListNode p=fakeHead;
while(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!=null) p.next=l1;
if(l2!=null) p.next=l2;
return fakeHead.next;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
(2)递归:
//public class LeetCode21 为测试
public class LeetCode21 {
public static void main(String[] args) {
ListNode m1=new ListNode(1),m2=new ListNode(3),m3=new ListNode(5);
m1.next=m2;
m2.next=m3;
System.out.println("链表1:"+m1.val+"->"+m2.val+"->"+m3.val);
ListNode n1=new ListNode(2),n2=new ListNode(4),n3=new ListNode(6);
n1.next=n2;
n2.next=n3;
System.out.println("链表2:"+n1.val+"->"+n2.val+"->"+n3.val);
ListNode result=new Solution().mergeTwoLists(m1, n1);
if(result!=null){
System.out.print("合并链表:"+result.val);
ListNode resultNext=result.next;
while(resultNext!=null){
System.out.print("->"+resultNext.val);
resultNext=resultNext.next;
}
}
}
}
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(l1, l2.next);
return l2;
}
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
21. Merge Two Sorted Lists★的更多相关文章
- [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 ...
- 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 ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
- leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- 刷题21. Merge Two Sorted Lists
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- C# 写 LeetCode easy #21 Merge Two Sorted Lists
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- [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 (Easy)
合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...
- [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 ...
随机推荐
- 解决 ImportError: No module named _internal
参考: My pip is broken. _internal module cannot be imported. #5253 解决 ImportError: No module named _in ...
- The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration
用过Mysql的人都知道,这个时区问题真个磨人的小妖精,哪天一忘记设置了就会出来磨磨你!!! 之前用的解决方法都是在Mysql的配置上添加与时区相关的配置,但是今天看到一篇博客:https://blo ...
- Spring Cloud 升级最新 Finchley 版本,踩了所有的坑!
Spring Boot 2.x 已经发布了很久,现在 Spring Cloud 也发布了 基于 Spring Boot 2.x 的 Finchley 版本,现在一起为项目做一次整体框架升级. 升级前 ...
- 【原】HDMI输出接口传输速率计算
1.1080P60为例: 三组差分线 R.G.B,每组速率: R:1920x1080(像素)x10(有效位为8bit,按10bit传输)x60(帧率)= 1244160000 ~~1.25G bit/ ...
- 未在本地计算机上注册microsoft.ace.12.0的解决办法
(1)去http://download.microsoft.com/download/7/0/3/703ffbcb-dc0c-4e19-b0da-1463960fdcdb/AccessDatabase ...
- python中对文件和文件夹的操作
一.说明 python中主要通过os模块和shutil模块两个模块对文件进行相关操作,移动.复制.删除.重命名.比较大的文件通过命令操作可以节省时间,提高效率. 二.实例对文件夹中文件的拷贝 from ...
- 记录Datagrid使用的一些事项
1.将两个列的文本拼接到一起并显示,如列1为name,列2为code,需要显示name(code).如:小明(123) 则初始化datagrid时在columns[]里加入:列1添加formatter ...
- C++标准模板库(STL)之Stack
1.Stack的常用用法 stack:栈,一个后进先出的容器. 1.1.stack的定义 加上头文件#include<stack>和using namespace std; stack&l ...
- idea函数被调用
打开一个复杂的程序或者项目进行分析的时候,我们就需要知道一个方法在哪里被调用,用于迅速厘清代码逻辑.操作如下:选中函数,右键,点击Find Usages. 如图: 操作简单,但右键还是没有快捷键方便. ...
- ARP协议分析(Wireshark)
一.说明 1.1 背景说明 以前学网络用的谢希仁的<计算机网络原理>,一是网开始学不太懂网络二是ARP协议是没有数据包格式的(如果没记错应该是没有).学完只记得老师说:ARP很简单的,就是 ...