这个非常简单的题目,题目如下:

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.

就是算导里面的第一个算法讲解,但是我的C++水平实在太渣,所以对于链表的操作很蠢,但还好完成了。题解如下:

/**
* 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 && l2 == NULL)
{
return NULL;
} if (l1 == NULL || l2 == NULL)
{
return (l1 == NULL) ? (l2) : (l1);
}
ListNode *aspros, *deferos, *root;
aspros = deferos = new ListNode(0);
if (l1->val <= l2->val)
{
aspros->val = l1->val;
aspros->next = NULL;
l1 = l1->next;
}
else
{
aspros->val = l2->val;
aspros->next = NULL;
l2 = l2->next;
}
root = aspros; while (l1 && l2)
{
if (l1->val <= l2->val)
{
aspros = new ListNode(0);
aspros->val = l1->val;
aspros->next = NULL;
deferos->next = aspros;
deferos = aspros;
l1 = l1->next;
}
else
{
aspros = new ListNode(0);
aspros->val = l2->val;
aspros->next = NULL;
deferos->next = aspros;
deferos = aspros;
l2 = l2->next;
}
} while (l1)
{
aspros = new ListNode(0);
aspros->val = l1->val;
aspros->next = NULL;
deferos->next = aspros;
deferos = aspros;
l1 = l1->next;
} while (l2)
{
aspros = new ListNode(0);
aspros->val = l2->val;
aspros->next = NULL;
deferos->next = aspros;
deferos = aspros;
l2 = l2->next;
} return root;
}
};

这个就是因为我不会初始化链表,所以在一开始的地方非常蠢,作了两个判断,一个是l1与l2都为NULL的情况,另一个是它俩有一个是空的情况,可以直接弹出,接着就是合并了。

[leetcode] 17. Merge Two Sorted Lists的更多相关文章

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

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

  3. 蜗牛慢慢爬 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 ...

  4. LeetCode 23 Merge k Sorted Lists(合并k个有序链表)

    题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...

  5. [Leetcode Week4]Merge Two Sorted Lists

    Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...

  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] 23. Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  8. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

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

随机推荐

  1. Ansiable Manage MySQL global variables

    mysql_variables - Manage MySQL global variables New in version 1.3. Synopsis Requirements (on host t ...

  2. MySql的基本架构演变

    [MySql的基本架构演变] 没有并发的增长,也就没有必要做高可扩展性的架构. Scale-up :  纵向扩展,通过替换为更好的机器和资源来实现伸缩,提升服务能力 Scale-out : 横向扩展, ...

  3. 'org.springframework.beans.factory.xml.XmlBeanFactory' is deprecated

    'org.springframework.beans.factory.xml.XmlBeanFactory' is deprecated XmlBeanFactory这个类已经被摒弃了.可以用以下代替 ...

  4. for of 与 for in 的区别

    遍历数组通常使用for循环,ES5的话也可以使用forEach,ES5具有遍历数组功能的还有map.filter.some.every.reduce.reduceRight等,只不过他们的返回结果不一 ...

  5. input限制数字输入

    onkeyup="this.value=this.value.replace(/\D/g,'')"

  6. iOS9 UIWindow rootViewController

    在iOS9中App被其他应用唤起的时候Crash,正常启动或者调试模式都不会Crash. 通过XCode - Window -Device,查看设备的log,如下 Assertion failure ...

  7. Spring框架的IOC核心功能快速入门

    2. 步骤一:下载Spring框架的开发包 * 官网:http://spring.io/ * 下载地址:http://repo.springsource.org/libs-release-local/ ...

  8. Django配置Bootstrap, js

    1.首先在APP目录下创建一个static文件夹 如图: # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'dj ...

  9. UI设计不就是画线框,凭什么年薪30W?

    作为一枚界面设计师 我真的很想为UI设计抱不平啊!! UI设计真是一个备受不解的职业 常会被误解,然后出现以下场景 程序欧巴: 界面画好没?按钮圆的方的不都能用吗?纠结那多干嘛? 产品经理: 这次我们 ...

  10. laravel的foreach

    1.控制器 2.模板