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. python中Scikit-Learn机器学习模块

    Scikit-Learn是基于python的机器学习模块,基于BSD开源许可证.这个项目最早由DavidCournapeau 在2007 年发起的,目前也是由社区自愿者进行维护. Scikit-Lea ...

  2. GIT warning: LF will be replaced by CRLF.

    git config --global core.autocrlf false git config --global core.autocrlf false  

  3. php注意事项

    1. 不要使用mysql_函数 这一天终于来了,从此你不仅仅"不应该"使用mysql_函数.PHP 7 已经把它们从核心中全部移除了,也就是说你需要迁移到好得多的mysqli_函数 ...

  4. 运行js提示库没有注册错误8002801d的解决办法

    运行js提示库没有注册错误8002801d的解决办法这个错误主要是因为服务器上的windows scripts版本较低,请按下面的链接下载较高版本windows scripts 5.6并在服务器上进行 ...

  5. 用python的BeautifulSoup分析html 【转】

    原地址:http://www.cnblogs.com/twinsclover/archive/2012/04/26/2471704.html 序言 之前用python爬取网页的时候,一直用的是rege ...

  6. Windows Store App 变形特效

    在应用程序的开发过程中,为了让界面按照期望的效果显示,有时会对界面元素应用变形特效,例如图片的缩放.旋转.移动等.与3D特效不同,在界面元素实现变形特效之后,其平行关系不会发生改变,只不过是位置.大小 ...

  7. iOS开发拓展篇—音频处理(音乐播放器2)

    iOS开发拓展篇—音频处理(音乐播放器2) 说明:该文主要介绍音乐播放界面的搭建. 一.跳转 1.跳转到音乐播放界面的方法选择 (1)使用模态跳转(又分为手动的和自动的) (2)使用xib并设置跳转 ...

  8. java 解决汉诺塔问题

    //汉诺塔问题//HanYang 2016/10/15 import java.util.Scanner; //输出public class Hanuota { public static void ...

  9. 如何使用 WinInet 时提供下载上载进度信息

    概要许多开发人员都使用 WinInet 函数来下载或上载文件在 Internet 上的想要提供一个进度条以指示多少文件传输已完成,但多少就越长.您可以使用以下机制来完成此.Collapse image ...

  10. xxxxxxxxx

    异步for (var index = 0; index < data.length; index++) { var req = http.request(urlEntity, function( ...