一.题目

Merge Two Sorted Lists

Total Accepted: 63974 Total Submissions: 196044My
Submissions

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.

Show Tags
Have you met this question in a real interview?

Yes
No

Discuss










二.解题技巧

    这道题就是将两个已排序的列表的元素进行比較,当某一个列表的元素比較小的话。就将其增加到输出列表中。并将该列表的指针指向列表的下一个元素。这道题是比較简单的,可是有一个边界条件要注意,就是两个列表可能会出现为空的情况,假设l1为空时,能够直接将l2进行返回;假设l2为空时,能够直接将l1返回,这样能够降低非常多计算量。



三.实现代码

#include <iostream>

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/ 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;
} ListNode Head(0);
ListNode *Pre = &Head; while(l1 && l2)
{
if (l1->val < l2->val)
{
Pre->next = l1;
l1 = l1->next;
Pre = Pre->next;
}
else
{
Pre->next = l2;
l2 = l2->next;
Pre = Pre->next;
}
} while (l1)
{
Pre->next = l1;
l1 = l1->next;
Pre = Pre->next;
} while(l2)
{
Pre->next = l2;
l2 = l2->next;
Pre = Pre->next;
} return Head.next; }
};



四.体会

   这道题主要考察的就是边界条件,主要就是处理链表为空的情况,也就是,假设l1为空。就返回l2,假设l2为空,就直接返回l1。

简单的题要考虑充分啊。




版权全部,欢迎转载。转载请注明出处,谢谢




LeetCode_Merge Two Sorted Lists的更多相关文章

  1. [LeetCode] Merge k Sorted Lists 合并k个有序链表

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

  2. [LeetCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  3. [LintCode] Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...

  4. No.023:Merge k Sorted Lists

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

  5. Merge k Sorted Lists

    1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...

  6. 71. Merge k Sorted Lists

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

  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. Merge Two Sorted Lists

    Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked ...

  9. Java for LeetCode 023 Merge k Sorted Lists

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

随机推荐

  1. oracle数据库中insert与select

    前几天遇到了一个问题,insert语句中,不仅要insert页面传过来的值,还要insert select的值. 语句应该这样写: insert into 表1(字段a,字段b,...) select ...

  2. Lisp学习:这是本质与应用?

    The Common Lisp Programming Language "The programming language of choice for those who set out ...

  3. 09-使用for循环输出空心菱形(循环)

    /** * 使用for循环输出空心菱形 * */ public class Test7 { public static void main(String[] args) { for (int i = ...

  4. 重操JS旧业第十弹:闭包

    闭包是js最难理解,也是最蛋疼的一个名词,仿佛只可意会不可言传一样,有人说闭包说白了就是函数嵌套,也有人说闭包就是函数能够访问函数外部的变量,而内部的外部访问不了: 貌似都非常有道理,其实仔细想来只不 ...

  5. 文本面板——axure线框图部件库介绍

    文本部件用于在页面中显示文字,对于文字的格式可以随意的更改,设定不同的字体.尺寸和颜色. 特别注意:文本面板的高度无法直接调整,它的高度是随着字体的大小自动变化的 在6.5版本中,对文本的排版都有2个 ...

  6. IIS Web服务扩展中添加ASP.NET4.0

    问题 服务器上安装了ASP.NET 4.0.30319组件,但是在IIS的Web服务扩展中并没有找到ASP.NET v4.0.30319这项,这导致基于.NET4.0开发的网页都无法正常浏览(404错 ...

  7. 如何在SourceInsight中选中匹配的大括号中的内容

    如何在SourceInsight中选中匹配的大括号中的内容 要分析的代码很长,多个for,if等分析嵌套在一起,代码有点乱,找到了这个分支的头,却不知道尾在哪,找到了尾却不知道哪是开头,在网上找了一下 ...

  8. 动静结合学内核:linux idle进程和init进程浅析

    刘柳 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 + titer1@qq.com 退休的贵族进程 ...

  9. 几本不错的CPU设计以及VLSI的书籍

    1. Microprocessor Design Principales and Practrices with VHDL  特点:电路与VHDL一一对应,比较清楚,而且还用MAX+plus进行仿真 ...

  10. UIPageControl的一个Demo

    本篇文章只是记录了一个自定义UIPageControl 的制作,其实很简单的   源码在底部会奉上链接,还望多多交流,多多支持. 首先效果图如下: 首先先做一个UISCrollView // 建议这样 ...