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.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

题解:

  简单的链表遍历,还可用递归做。

Solution 1

 v/**
* 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) {
ListNode dummy(-);
ListNode* cur = &dummy; while(l1 && l2) {
if (l1->val < l2->val) {
cur->next = l1;
l1 = l1->next;
} else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
} cur->next = l1 ? l1 : l2; return dummy.next;
}
};

  递归方法

Solution 2

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

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

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

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

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

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

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

  4. 【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 spli ...

  5. 【LeetCode】023. Merge k Sorted Lists

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

  6. 【LeetCode】23. Merge k Sorted Lists

    合并k个已合并链表. 思路:先把链表两两合并,直到合并至只有一个链表 /** * Definition for singly-linked list. * struct ListNode { * in ...

  7. 【一天一道LeetCode】#23. Merge k Sorted Lists

    一天一道LeetCode系列 (一)题目 Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  8. 【一天一道LeetCode】#21. Merge Two Sorted Lists

    一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...

  9. 【原创】leetCodeOj --- Merge k Sorted Lists 解题报告

    题目地址: https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题目内容: /** * Definition for singly-linke ...

随机推荐

  1. js城市联动选择器

    <html> <head> <META charset="utf8"> <script type="text/javascrip ...

  2. Django 之Ajax&Json&CORS&同源策略&Jsonp用法

    什么是Json 定义: JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.它基于 ECMAScript (w3c制定的js规范)的一个子集 ...

  3. PAT 1058. 选择题(20)

    批改多选题是比较麻烦的事情,本题就请你写个程序帮助老师批改多选题,并且指出哪道题错的人最多. 输入格式: 输入在第一行给出两个正整数N(<=1000)和M(<=100),分别是学生人数和多 ...

  4. Java 迭代器

    1.快速报错 public class FailFast { public static void main(String[] args) { List<String> c = new A ...

  5. wecenter 问答社区 dockerfile,不用纠结于物理机的运行环境

    FROM webdevops/php-nginx:centos-7-php56 ADD . /app RUN ["chmod", "777", "/a ...

  6. Kindeditor API

    根据map规则删除range中的element或attribute. cmd.remove({ span : '*', div : 'class,border' });   commonNode(ma ...

  7. [转]详解Java解析XML的四种方法

    XML现在已经成为一种通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便.对于XML本身的语法知识与技术细节,需要阅读相关的技术文献,这里面包括的内容有DOM ...

  8. 第五章 python中的异常处理

    每种编程语言都会有自己的异常处理机制,虽然各有特色,但基本上都差不多,那么python中强大异常处理机制是什么样的呢? 一.异常: python用异常对象来表示异常情况,遇到错误后,会引发异常.如果异 ...

  9. 顺序表的静态存储(C语言实现)

    顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构.  1.顺序表的结构体声明 #define MAX_SIZE 5 //定义数组的大小 typed ...

  10. poj 3278 Catch That Cow-搜索进阶-暑假集训

    Catch That Cow Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...