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. PHP 小方法之 仿百度蜘蛛采集

    if(!function_exists('_GetContent')){ function _GetContent( $url ){ $ch = curl_init(); $ip = '220.181 ...

  2. ArrayList 如何增加大小

    JDK1.8 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; transient Object[] elem ...

  3. 经典SQL语句大全以及50个常用的sql语句

    经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql serv ...

  4. c#遍历并判断实体或类的成员属性

    c#的Attribute有些类似java中的annotation,可以方便地在类成员中做修饰/限制作用. Demo: class ss { public stat BsonDocument Itera ...

  5. c/c++ 软件集成 安装和可卸载软件

    作为一个工程师应具备的一些能力: 1. 首先具备这款软件:  >inno  Setup      免费版还开源,良心货,妥妥的. 2. 这款软件上手也比较款,可自行参考使用文档 3.编译成功,生 ...

  6. 终于解决了IE8不支持数组的indexOf方法,array的IndexOf方法

    /* 终于解决了IE8不支持数组的indexOf方法 */ if (!Array.prototype.indexOf) { Array.prototype.indexOf = function (el ...

  7. PostgreSQL的时间/日期函数使用 转

    http://www.cnblogs.com/mchina/archive/2013/04/15/3010418.html

  8. ListView.DragEnter触发不了

    经过千百度的搜索之后,终于找到了一点线索,原文是:https://msdn.microsoft.com/en-us/magazine/mt185571.aspx 有能力的可以参阅原文,想省事的可以等待 ...

  9. HTML5基本元素初探

    最近看了一些HTML5的基础知识,写了一些小案例,记录一下,方便查找. 1.新建的HTML5页面中显著的变化是:DOCTYPE声明变简洁(<!DOCTYPE html>) / <me ...

  10. nginx全局变量实例对照 rewrite参考手册

    http://dwz.stamhe.com/index.php?_a=index&_m=show&count=10 remote_addr 客户端ip,如:192.168.4.2 bi ...