LeetCode_Merge Two Sorted Lists
一.题目
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.
二.解题技巧
三.实现代码
#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; }
};
四.体会
简单的题要考虑充分啊。
LeetCode_Merge Two Sorted Lists的更多相关文章
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [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 ...
- [LintCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list sh ...
- No.023:Merge k Sorted Lists
问题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexit ...
- Merge k Sorted Lists
1. Merge Two Sorted Lists 我们先来看这个 问题: Merge two sorted linked lists and return it as a new list. The ...
- 71. Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- Merge Two Sorted Lists
Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked ...
- 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. 解 ...
随机推荐
- oracle数据库中insert与select
前几天遇到了一个问题,insert语句中,不仅要insert页面传过来的值,还要insert select的值. 语句应该这样写: insert into 表1(字段a,字段b,...) select ...
- Lisp学习:这是本质与应用?
The Common Lisp Programming Language "The programming language of choice for those who set out ...
- 09-使用for循环输出空心菱形(循环)
/** * 使用for循环输出空心菱形 * */ public class Test7 { public static void main(String[] args) { for (int i = ...
- 重操JS旧业第十弹:闭包
闭包是js最难理解,也是最蛋疼的一个名词,仿佛只可意会不可言传一样,有人说闭包说白了就是函数嵌套,也有人说闭包就是函数能够访问函数外部的变量,而内部的外部访问不了: 貌似都非常有道理,其实仔细想来只不 ...
- 文本面板——axure线框图部件库介绍
文本部件用于在页面中显示文字,对于文字的格式可以随意的更改,设定不同的字体.尺寸和颜色. 特别注意:文本面板的高度无法直接调整,它的高度是随着字体的大小自动变化的 在6.5版本中,对文本的排版都有2个 ...
- IIS Web服务扩展中添加ASP.NET4.0
问题 服务器上安装了ASP.NET 4.0.30319组件,但是在IIS的Web服务扩展中并没有找到ASP.NET v4.0.30319这项,这导致基于.NET4.0开发的网页都无法正常浏览(404错 ...
- 如何在SourceInsight中选中匹配的大括号中的内容
如何在SourceInsight中选中匹配的大括号中的内容 要分析的代码很长,多个for,if等分析嵌套在一起,代码有点乱,找到了这个分支的头,却不知道尾在哪,找到了尾却不知道哪是开头,在网上找了一下 ...
- 动静结合学内核:linux idle进程和init进程浅析
刘柳 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 + titer1@qq.com 退休的贵族进程 ...
- 几本不错的CPU设计以及VLSI的书籍
1. Microprocessor Design Principales and Practrices with VHDL 特点:电路与VHDL一一对应,比较清楚,而且还用MAX+plus进行仿真 ...
- UIPageControl的一个Demo
本篇文章只是记录了一个自定义UIPageControl 的制作,其实很简单的 源码在底部会奉上链接,还望多多交流,多多支持. 首先效果图如下: 首先先做一个UISCrollView // 建议这样 ...