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.
题目标签:Linked List
题目给了我们两个lists,让我们有序的合并两个 lists。
这题利用递归可以从list 的最后开始向前链接nodes,代码很简洁,清楚。
Java Solution:
Runtime beats 74.35%
完成日期: 06/09/2017
关键词:singly-linked list
关键点:利用递归从最后开始链接nodes
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
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;
}
} }
参考资料:
https://discuss.leetcode.com/topic/45002/java-1-ms-4-lines-codes-using-recursion
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 21. Merge Two Sorted Lists (合并两个有序链表)的更多相关文章
- leetcode 21 Merge Two Sorted Lists 合并两个有序链表
描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...
- LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- [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 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)
这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...
- 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 ...
- [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]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 ...
- 021 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 ...
随机推荐
- Python-Day07-图形用户界面和游戏开发
Python-100Day-学习打卡Author: Seven_0507Date: 2019-05-22123 文章目录Python图形用户界面和游戏开发1. tkinter模块2. Pygame进行 ...
- 设置vscode为中文
设置vscode为中文 ctr+shift+p 输入 configure language 进 en更改为zh-cn , 重启vscode即可 , 如果还不行,就安装插件
- c++类简介
C++类(Class)总结 一.C++类的定义 C++中使用关键字 class 来定义类, 其基本形式如下:class 类名{ public: //行为或属性 protected: // ...
- mfc按钮悬停显示文字
.h CToolTipCtrl m_toopTip; .cpp oninitdialog void CDlgDwgLibMan::InitTooltips(){ EnableToolTips(); m ...
- The following packages have unmet dependencies:
root@ubuntu:~# apt-get install open-iscsiReading package lists... DoneBuilding dependency treeReadin ...
- freemarker使用map替换ftl中相关值
ftl文件demo01.ftl <html> <head> <title>Welcome!</title> </head> <body ...
- vue+webpack+npm搭建的纯前端项目
转载来源:https://www.cnblogs.com/shenyf/p/8341641.html 搭建node环境 下载 1.进入node.js官方网站下载页,点击下图中框出位置,进行下载即可,当 ...
- Python 爬虫爬取今日头条街拍上的图片
# 今日头条--街拍 import requests from urllib.parse import urlencode import os from hashlib import md5 from ...
- JAVA基础——Date和Calendar类
1计算某一月份的最大天数 Calendar time=Calendar.getInstance(); time.clear(); time.set(Calendar.YEAR,year); //yea ...
- 在Java中调用带参数的存储过程
JDBC调用存储过程: CallableStatement 在Java里面调用存储过程,写法那是相当的固定: Class.forName(.... Connection conn = DriverMa ...