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 * root = new ListNode(-);
ListNode * helper = root;
while(l1!=NULL && l2!=NULL){
if(l1->val <= l2->val)
root->next = l1, l1=l1->next;
else if(l1->val > l2->val)
root->next = l2, l2=l2->next;
root = root->next;
}
while(l1!=NULL){
root->next = l1;
l1 = l1->next;
root = root->next;
}
while(l2!=NULL){
root->next = l2;
l2 = l2->next;
root = root->next;
}
return helper->next;
}
};
 public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode helper = new ListNode(0);
ListNode ret = helper;
if(l1 == null) return l2;
if(l2 == null) return l1;
while(l1 != null && l2 != null){
if(l1.val < l2.val){
helper.next = l1;
l1 = l1.next;
}else{
helper.next = l2;
l2 = l2.next;
}
helper = helper.next;
}
while(l1 != null){
helper.next = l1;
l1 = l1.next;
helper = helper.next;
}
while(l2 != null){
helper.next = l2;
l2 = l2.next;
helper = helper.next;
}
return ret.next;
}
}

LeetCode OJ:Merge Two Sorted Lists(合并两个链表)的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

  5. leetcode 21 Merge Two Sorted Lists 合并两个有序链表

    描述: 合并两个有序链表. 解决: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; if (!l2) ...

  6. [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 ...

  7. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  8. [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 ...

  9. 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 list sh ...

  10. [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 ...

随机推荐

  1. tomcat 是如何处理jsp和servlet请求

    我们以一个具体的例子,来跟踪TOMCAT, 看看它是如何把Request一层一层地递交给下一个容器, 并最后交给Wrapper来处理的. 以http://localhost:8080/web/logi ...

  2. 008-shiro与spring web项目整合【二】认证、授权、session管理

    一.认证 1.添加凭证匹配器 添加凭证匹配器实现md5加密校验. 修改applicationContext-shiro.xml: <!-- realm --> <bean id=&q ...

  3. 【Navicat连接Oracle数据库】-Navicat连接Oracle数据库设置

    1.navicat连接数据配置信息如下图所示:   点击"确定"按钮,进入到软件   按照图中所画的步骤顺序操作,最后重新启动navicat就可. 关于里面的这个文件夹 insta ...

  4. Linux学习笔记—vim程序编辑器

    vi和vim vim是vi的升级版,支持vi的所有指令 vi的使用 vi分为三种模式:一般模式.编辑模式.命令行模式 一般模式 以vi打开一个文件就直接进入一般模式了,这个模式下可以使用上下左右按键来 ...

  5. tornado下模板引擎的使用

    模板引擎 Tornado中的模板语言和django中类似,模板引擎将模板文件载入内存,然后将数据嵌入其中,最终获取到一个完整的字符串,再将字符串返回给请求者. Tornado =的模板支持“控制语句” ...

  6. 记一次centos7挂在nas盘的踩坑经过

    p:first-child, #write > ul:first-child, #write > ol:first-child, #write > pre:first-child, ...

  7. 使用pycharm操作django

    新建项目,选择已经建立好的虚拟环境 进入指令界面 新建app 添加一些文件和文件夹用于以后存放各种数据 settings设置 TEMPLATES设置 TEMPLATES = [ { 'BACKEND' ...

  8. 17届计算机应届生秋季校招分享 to Tomorrow

    首先自我介绍一下,本人来自普通二本院校,计算机科学与技术专业,在校有一到两年asp.net项目经验,花了两个星期左右的时间转向java.现将此次的求职经历,分为三阶段,分享给大家. First Sta ...

  9. 求组合数的方法:转载自VincentCZW的博客

    遇到了就查了下:地址:http://www.cnblogs.com/BeyondAnyTime/archive/2012/05/18/2508189.html 求一个组合数Cnm的值,Cnm= n! ...

  10. redis 系列文章推荐

    推荐博客: Redis在linux上的安装: http://www.open-open.com/lib/view/open1426468117367.html Redis的三种启动方式: http:/ ...