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

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. js中常见的创建对象的方法

    前两天好好的把高程对象那一块又读了下,顺便写点笔记.补一句:代码都测试过了,应该没有问题的.可以直接拿到控制台跑! 1.工厂模式 function person(name, age, job) { v ...

  2. tensorflow生成随机数的操作 tf.random_normal & tf.random_uniform & tf.truncated_normal & tf.random_shuffle

    tf.random_normal 从正态分布输出随机值. random_normal(shape,mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name ...

  3. https 证书传递、验证和数据加密、解密过程解析

    我们都知道HTTPS能够加密信息,以免敏感信息被第三方获取.所以很多银行网站或电子邮箱等等安全级别较高的服务都会采用HTTPS协议. HTTPS简介 HTTPS其实是有两部分组成:HTTP + SSL ...

  4. Docker Dockerfile 定制镜像(转)

    转自: https://yeasy.gitbooks.io/docker_practice/ 及 https://blog.csdn.net/wo18237095579/article/details ...

  5. 前端知识--------HTML内容

    HTML介绍 1.web服务本质 import socket sk = socket.socket() sk.bind(('127.o.o.1',8080)) sk.listen() while 1: ...

  6. Honeycomb

    Honeycomb http://codeforces.com/gym/102028/problem/F time limit per test 4.0 s memory limit per test ...

  7. 【vs2010】转换到 COFF 期间失败: 文件无效或损坏

    不知怎么本来编译好好的VS2010环境,忽然出现“转换到COFF 期间失败:文件无效或损坏”的链接错误.花了好多天,试了好多方法,最终解决了这个问题. 现在罗列一下这几种解决方案: 方案1:      ...

  8. php SESSON共享 (mysql方式)

    为什么要进行session共享? 因为一些大型网站,通常会有很多服务器,每个服务器运行不同的业务模块,并使用二级域名(或是完全不同的域名),而用户系统是统一的,通过登陆名.密码来登陆各模块.用户数据放 ...

  9. SQL Cursor 基本用法[用两次FETCH NEXT FROM INTO语句?]

    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> ...

  10. Sql自定义表类型批量导入数据

    -- 创建自定义表类型 CREATE TYPE [dbo].[App_ProductTable] AS TABLE( [p_name] [varchar](50) NOT NULL, [p_audio ...