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.

思路.首先判断是否有空链表,如果有,则直接返回另一个链表,如果没有,则开始比较两个链表的当前节点,返回较小的元素作为前驱,并且指针向后移动一位,再进行比较,如此循环,知道一个链表的next指向NULL,将另一个链表的后序元素进行连接即可。

迭代:

/**
* 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; if(l1->val < l2->val) {
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
l2->next = mergeTwoLists(l2->next, l1);
return l2;
}
}
};

循环:

/**
* 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* head = (l1->val > l2->val)?l2:l1;
ListNode* temp;
while(l1!=NULL && l2!=NULL)
{
while(l1->next!=NULL&&l2!= NULL &&l1->next->val <= l2->val)
{
l1 = l1->next;
}
if(l1 != NULL&&l2 !=NULL &&l1->val <= l2->val)
{
temp = l1->next;
l1->next = l2;
l1 = temp;
}
while(l2->next !=NULL&& l1 !=NULL &&l2->next->val <= l1->val)
{
l2 = l2->next;
}
if(l2 != NULL &&l1 != NULL&&l2->val <= l1->val)
{
temp = l2->next;
l2->next = l1;
l2 = temp;
} }
return head;
}
};

LeetCode 【21. Merge Two Sorted Lists】的更多相关文章

  1. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

  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. LeetCode:21. Merge Two Sorted Lists(Easy)

    1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...

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

  6. leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)

    1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...

  7. 21. Merge Two Sorted Lists(合并2个有序链表)

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

  8. LeetCode LinkList 23. Merge k Sorted Lists

    这两天一直也没有顾上记录一下自己做过的题目,回头看看,感觉忘的好快,今天做了一个hard,刚开始觉得挺难得,想了两种方法,一种是每次都从k个list中选取最小的一个,为空的直接跳过,再就是每次合并其中 ...

  9. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

随机推荐

  1. 12款最佳Linux命令行终端工具, 20款优秀的 Linux 终端仿真器

    12款最佳Linux命令行终端工具     如果你跟我一样,整天要花大量的时间使用Linux命令行,而且正在寻找一些可替代系统自带的老旧且乏味的终端软件,那你真是找对了文章.我这里搜集了一些非常有趣的 ...

  2. sqlloader导出数据和导入数据

    分类: Oracle 忙了一天终于把sqlloader导出数据和导入数据弄清楚了,累死俺了... 这个总结主要分为三个大部分,第一部分(实例,主要分两步),第二部分(参数小总结),第三部分(完全参数总 ...

  3. win安装NLTK出现的问题

    一.今天学习Python自然语言处理(NLP processing) 需要安装自然语言工具包NLTK Natural Language Toolkit 按照教程在官网https://pypi.pyth ...

  4. Deep Learning 11_深度学习UFLDL教程:数据预处理(斯坦福大学深度学习教程)

    理论知识:UFLDL数据预处理和http://www.cnblogs.com/tornadomeet/archive/2013/04/20/3033149.html 数据预处理是深度学习中非常重要的一 ...

  5. Deep Learning 1_深度学习UFLDL教程:Sparse Autoencoder练习(斯坦福大学深度学习教程)

    1前言 本人写技术博客的目的,其实是感觉好多东西,很长一段时间不动就会忘记了,为了加深学习记忆以及方便以后可能忘记后能很快回忆起自己曾经学过的东西. 首先,在网上找了一些资料,看见介绍说UFLDL很不 ...

  6. CAD打开文件总是弹出要求选择字体怎么办

    CAD打开文件总是弹出要求选择字体怎么办1.在C:\Documents and Settings\下搜索acad.fmp文件,双击用记事本打开acad.fmp文件,在最后添加内容,上面几行本来就有,不 ...

  7. ajax json 动态传值

    <a href="#" onclick="getRightInfo(${v.ctid})"></a> <div id=" ...

  8. ASP.NET jquery.uploadify上传控件中文乱码解决办法(转)

    原文地址:http://blog.csdn.net/ningxi_/article/details/6234725 在一般处理程序上加上这几句话: context.Response.ContentTy ...

  9. 安装64位mysql5.626

    计算机--右击属性--左上高级系统变量---环境变量 path 添加 mysql 的bin目录 ;D:\mysqlwinx64\bin1 //mysql 5.6.26安装前先解压到d盘根目录 cd D ...

  10. 启动本地Oracle

    net start OracleOraDb10g_home1TNSListenernet start OracleServiceORCL第一个是监听服务第二个是数据库服务