【Leetcode】【Easy】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、判断l1和l2是否有效
2、新建preHead结点,newlist指针指向preHead;
3、循环开始,满足l1和l2都不为空:
(1)比较当前l1和l2的值,将较小的穿进newlist中
4、newlist链接l1或l2的剩余部分
5、释放表头,返回newlist;
/**
* 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* prehead = new ListNode();
ListNode* newlist = prehead; while (l1 != NULL && l2 != NULL) {
if (l1->val > l2->val) {
newlist->next = l2;
l2 = l2->next;
} else {
newlist->next = l1;
l1 = l1->next;
}
newlist = newlist->next;
} if (l1 == NULL) newlist->next = l2;
else newlist->next = l1; l1 = prehead->next;
delete prehead;
return l1;
}
};
【Leetcode】【Easy】Merge Two Sorted Lists .的更多相关文章
- LeetCode Linked List Easy 21. Merge Two Sorted Lists
Description Merge two sorted linked lists and return it as a new list. The new list should be made b ...
- 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
题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...
- 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 ...
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
- 【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练习题】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【LeetCode】【数组归并】Merge k Sorted Lists
描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
随机推荐
- Python+Selenium之cannot focus element 解决方法
有时候刚进入页面输入第一个值时脚本会报错:cannot focus element 贴下我的脚本和解决办法供大家参考 我原本的脚本是: WebDriverWait(driver,15,0.5).unt ...
- 安装APK时报 Installation failed with message Failed to finalize session : INSTALL_FAILED_USER_RESTRICTED: Invalid apk.
Installation failed with message Failed to finalize session : INSTALL_FAILED_USER_RESTRICTED: Invali ...
- How to remove constantly launching services on Mac OS X
Even after you uninstall it, some Mac OS X software just won’t quit nagging you or notifying you of ...
- DBCP连接池工具类模板
package ${enclosing_package}; import java.io.InputStream; import java.sql.Connection; import java.sq ...
- 案例17-validate自定义校验规则校验验证码是否输入正确
1 自定义校验规则代码 <script type="text/javascript"> //使用validate插件进行表单的校验 $(function(){ $(&q ...
- MySQL查询近一个月的数据
MySQL查询近一个月的数据 近一个月统计SQL select user_id, user_name, createtime from t_user where DATE_SUB(CURDATE(), ...
- wordpress编辑器选择ckeditor、ckfinder
CKEditor for WordPress 搜索安装 上传功能需要ckfinder 下载 CKFinder for PHP: http://ckfinder.com/download 上传ckfin ...
- web_02Java ee实现验证码,网站访问次数功能
Web Web_02版本: 实现功能 1,验证码 2,网站访问次数统计 设计内容 1,servlet 2,jsp 3,js *重点 1,验证码相关: 1,Servlrt类实现验证码的生成 CheckC ...
- 移动端手势事件 hammer.JS插件
一.引入hammer.JS 1.下载地址:http://download.csdn.net/detail/webxiaoma/9872249 2.官网地址:http ...
- Java入门系列-18-抽象类和接口
抽象类 在第16节继承中,有父类 People People people=new People(); people.sayHi(); 实例化People是没有意义的,因为"人"是 ...