leetcode [64] merge tow sorted lists
之前忘记记录这题了,现在补上。
合并两个有序的list,要求是:
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) return l2;
if (l2 == NULL) return l1;
ListNode *tmp = new ListNode();
ListNode *head = tmp;
while(l1 && l2)
{
if (l1 -> val <= l2 -> val)
{
tmp -> next = l1;
tmp = l1;
l1 = l1 -> next;
}
else
{
tmp -> next = l2;
tmp = l2;
l2 = l2 -> next;
}
}
if (l1)
{
tmp -> next = l1;
}
else
{
tmp -> next = l2;
}
return head -> next;
}
};
leetcode [64] merge tow sorted lists的更多相关文章
- Java for LeetCode 023 Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...
- [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 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- LeetCode 23 Merge k Sorted Lists(合并k个有序链表)
题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...
- [Leetcode Week4]Merge Two Sorted Lists
Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...
- [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] 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】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists
Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...
随机推荐
- UVA 12538 Version Controlled IDE 解题报告
题意:给三种操作 1.在p位置插入一个字符串. 2.从p位置开始删除长度为c的字符串 3.输出第v个历史版本中从p位置开始的长度为c的字符串 解法:可以用平衡树做,但是不会.后来又听说可一用一个叫ro ...
- 3-08. 栈模拟队列(25)(ZJU_PAT 模拟)
主题链接:http://pat.zju.edu.cn/contests/ds/3-08 设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操 ...
- 使用Visual Studio 2010写Data Url生成工具C#版本
声明:本文系本人按照真实经历原创.未经许可,谢绝转载. 此文百度经验版本号:怎样用Visual Studio 2010打造Data Url生成工具 源代码下载:用Visual Studio 2010编 ...
- PAAS平台7×24小时可用性应用设计
如今非常多企业都在搭建自己的私有PAAS平台,当然也有非常多大型互联网公司搭建共同拥有PAAS平台(比如SAE/BAE/JAE(jae.jd.com)).那么使用PAAS平台来部署SAAS应用有哪些优 ...
- Oracle 中用一个表的数据更新另一个表的数据
Oracle 中用一个表的数据更新另一个表的数据 分类: SQL/PLSQL2012-05-04 15:49 4153人阅读 评论(1) 收藏 举报 oraclemergesubqueryinsert ...
- linux编curlDLL库so
转载请注明出处:帘卷西风的专栏(http://blog.csdn.net/ljxfblog) curl库是一个非常强大的http开源库.c++里面可以非常方便的和httpserver交互. 近期项目開 ...
- 学生表sid,sname,结果表cid,cname,学生成绩表sid,cid,cscore,最高要求的分数输出候补课程专门命名
--1.建表SQL: --学生表: -- Createtable createtable STUDENT ( SID NUMBERnotnull, SNAME NVARCHAR2) ) table ...
- Eclipse-----jrebel实现jetty热部署
步骤1:下载jrebel将文件解压缩到任意文件夹 步骤2:配置jetty watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVrZXdhbmd6aQ==/f ...
- Bootstrap(2)整体架构
Bootstrap(2)整体架构 大多数Bootstrap的使用者都认为Bootstrap只提供了CSS组件 和JavaScript插件,其实CSS组件和JavaScript插件只是Bootstrap ...
- TextView——setCompoundDrawables说明
Drawable drawable = mContext.getResources().getDrawable(R.drawable.duringtime); drawable.setBounds( ...