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.

解题:

题目比较简单,注意由于表头不确定,因此新建一个结点指向新创建的链表;

解题步骤:

1、判断l1和l2是否有效

2、新建preHead结点,newlist指针指向preHead;

3、循环开始,满足l1和l2都不为空:

  (1)比较当前l1和l2的值,将较小的穿进newlist中

4、newlist链接l1或l2的剩余部分

5、释放表头,返回newlist;

 /**
* 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* prehead = new ListNode();
ListNode* newlist = prehead; while (l1 != NULL && l2 != NULL) {
if (l1->val > l2->val) {
newlist->next = l2;
l2 = l2->next;
} else {
newlist->next = l1;
l1 = l1->next;
}
newlist = newlist->next;
} if (l1 == NULL) newlist->next = l2;
else newlist->next = l1; l1 = prehead->next;
delete prehead;
return l1;
}
};

【Leetcode】【Easy】Merge Two Sorted Lists .的更多相关文章

  1. LeetCode Linked List Easy 21. Merge Two Sorted Lists

    Description Merge two sorted linked lists and return it as a new list. The new list should be made b ...

  2. C# 写 LeetCode easy #21 Merge Two Sorted Lists

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

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

  4. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

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

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

  6. 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...

  7. 【leetcode】Merge k Sorted Lists

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

  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】【数组归并】Merge k Sorted Lists

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

  10. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

随机推荐

  1. 在W3C SCHOOL网站上发现一个关于Schema的错误

    原地址是http://www.w3school.com.cn/schema/schema_complex_empty.asp 下面这个例子是不正确的 xmlspy报错. 因为<xs:restri ...

  2. 并发编程之synchronize

    synchronized是Java中的关键字,是一种常用的线程同步锁. 用法 注意:在理解synchronized时,要知道一个核心点,synchronized锁定的不是代码,而是对象.使用synch ...

  3. SpringMVC的json交互

    一.注解说明 1.@RequestBody  作用:@RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内 ...

  4. html中 alt 和 title 的区别

    alt 用来给图片来提示的(图片载入失败时以文本形式提示). Title用来给链接文字或普通文字提示的(在鼠标放上去的时候就会提示).

  5. Java入门系列-16-继承

    这一篇文章教给新手学会使用继承,及理解继承的概念.掌握访问修饰符.掌握 final 关键字的用法. 继承 为什么要使用继承 首先我们先看一下这两个类: public class Teacher { p ...

  6. 学习Python要知道哪些重要的库和工具

    本文转自:https://github.com/jobbole/awesome-python-cn 环境管理 管理 Python 版本和环境的工具 p:非常简单的交互式 python 版本管理工具. ...

  7. Iterator和for...of循环

    Iterator和for...of循环 Iterator(遍历器)的概念 数据结构的默认Iterator接口 调用Iterator接口的场合 字符串的Iterator接口 Iterator接口与Gen ...

  8. 启停无线网卡bat脚本

    @echo off color 2 title 启停无线网卡 echo 启动无线网卡=======>按1键 echo 关闭无线网卡=======>按2键 set /p n= if /i & ...

  9. sql 递归 STUFF

    select distinct fm_id, ,,'') AS SO_Nums from [dbo].[t_BADItems] its 表内一对多 的关系查询

  10. 我的Chrome插件---纪录

    1.极简图床 写博客的时候用的上. 2.谷歌翻译 阅读英文文档直接选中翻译 3.OneTab 把当前网页集成一个tab,它有个好处就是,在写博客的时候,需要上不同的网站,写了一半有其他的事,这是可以集 ...