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. 浅谈HTML文档模式

    不知道爱多想的你有没有在编写HTML代码时思考过 <!DOCTYPE html> 或是这一长串看都看不懂的 <!DOCTYPE HTML PUBLIC "-//W3C//D ...

  2. redis实现队列queue

    参考:<Redis入门指南>第4章进阶 http://book.51cto.com/art/201305/395461.htm 4.4.2 使用Redis实现任务队列 说到队列很自然就能想 ...

  3. MySQL的搜索引擎,统一字符编码 和忘记MySQL密码如何破解

    忘记mysql密码 linux平台下,破解密码的两种方式 [root@egon ~]# rm -rf /var/lib/mysql/mysql #所有授权信息全部丢失!!! [root@egon ~] ...

  4. shiro 拦截器

    参考

  5. 8月自动化测试课程 - Selenium开源自动化测试实践

    8月自动化测试课程 - Selenium开源自动化测试实践 http://gdtesting.cn/news.php?id=35

  6. Linux常用命令(6/26)——dd命令和split命令

    dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. 以可选块长度复制文件,默认情况下从标准输入设备输出到标准输出设备.复制过程中,还可以对文件进行一些转换. dd命令可以指定block的 ...

  7. TCP的握手与挥手

    轻轻的TCP走了,正如TCP轻轻的来,TCP挥一挥手,传递了不知多少信息 看到哪,记到哪,想起哪,就看哪,这就是我的博客园,很随性 ---------------------------------- ...

  8. Makefile的简单编写【学习笔记】

    首先我们先创建两个简单的文件: main.c #include <stdio.h> extern void hi_fun(); int main() { printf("hell ...

  9. Spring_通过注解配置 Bean(1)

    beans-annotation.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns ...

  10. MySQL 数据(数据库)迁移

    查找MySQL真正的Data目录 show variables like 'datadir'; +---------------+-----------------+ | Variable_name ...