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. centos7 只需两步安装rabbitmq

    # yum -y install rabbitmq-server # systemctl start rabbitmq-server  && systemctl enable rabb ...

  2. linux下Tomcat shutdown无效

    问题: linux下Tomcat shutdown无效 linux下关闭tomcat后,发现重新启动Tomcat后.port号提示被占用, 原因: 这时可能是项目中的后台线程或者socket依旧在执行 ...

  3. 在用 JavaScript 工作时,我们经常和条件语句打交道,这里有5条让你写出更好/干净的条件语句的建议。

    1.多重判断时使用 Array.includes 2.更少的嵌套,尽早 return 3.使用默认参数和解构 4.倾向于遍历对象而不是 Switch 语句 5.对 所有/部分 判断使用 Array.e ...

  4. MySQL之 视图,触发器,事物,存储过程,函数(Day48)

    阅读目录 一.视图 二.触发器 三.事物 四.存储过程 五.函数 六.流程控制 一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名 ...

  5. 笔记-mysql 导出查询结果

    语法: The SELECT ... INTO OUTFILE 'file_name' [options] form of SELECT writes the selected rows to a f ...

  6. linux例行性工作调度学习(一)

    Linux系统中有一种例行性工作(crontab)可以调度,是通过crontab和at来实现的. 这两种工作调度: 一种是例行性的,就是每隔一定的周期要来办的事项. 一种是突发性的,就是这次做完以后就 ...

  7. phpword使用

    composer 安装 https://packagist.org/packages/phpoffice/phpword 开发文档:http://phpword.readthedocs.io/en/l ...

  8. 实验一 Linux初步认识

    遇到的困难和心得体会: 1.在操作过程中,有道作业是建立一个opt/forloutest的文件,而我建立了一个 OPT文件,cd OPT,却显示not a directory,通过阅读<linu ...

  9. C++命名(自定义)

    1.自定义函数 void GetName(): 2.布尔型变量 BOOL ISOPEN:

  10. MyEclipse/Eclipse中properties文件中文乱码问题解决

    有时候在myeclipse或者eclipse中打开properties文件时会发现其中的中文都是乱码.这是由于当前的properties文件编码格式不支持汉字造成的.当这种情况发生时,我们可以按照以下 ...