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. 解 ...
随机推荐
- codeforces 148D 概率DP
题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢. 王妃每次抓一仅仅老鼠,龙每次抓完一仅仅老鼠之后会有一仅仅老鼠跑出来. 每次抓老鼠和跑出来的老鼠都是随 ...
- Codility上的问题 (16) Omicron 2012
比较无聊的题,求斐波那契数的第N^M项. f(0) = 0, f(1) = 1, f(n) = f(n - 1) + f(n - 2),结果对10000103取模. N, M在[0..10^7]之间. ...
- hadoop拷贝文件时 org.apache.hadoop.ipc.RemoteException异常的解决
1.系统或hdfs是否有空间 2.datanode数是否正常 3.是否在safemode 4.防火墙关闭 5.配置方面 6.把NameNode的tmp文件清空,然后重新格式化NameNode
- CDialogBar(对话条)和CReBar(伸缩条)的编程
对话条是工具栏和无模式对话框相结合的产物,对话条和对话框类似,包含有标准的Windows控件,并且可以通过创建对话框模板来表示对话条. 伸缩条功能很强大,我们可以在伸缩条中直接增加CToolBar,C ...
- 关于yaf的控制器命名,一个纠结的问题(续)
以下方案缺少loader相关的步骤,明天补上!!! 前面写过一篇<关于yaf的控制器命名,一个纠结的问题>.没想到yaf群里面也有跟我遇到一样问题的人,分享下解决办法. 写完那篇博文后,我 ...
- OSGi 学习之路(4) - osgi的模块化 java在模块化的局限性
底层代码可见性控制 Java提供了private,public,protected和package private(无修饰符)这四种访问控制级别,不过这仅仅提供了底层的OO数据封装特性.包这个概念确实 ...
- HUNNU11342:Chemistry(模拟)
http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11342 Problem description The ch ...
- PowerDesigner反projectM连接ySql没有mySql odbc驱动器
PowerDesignerfang反project连接MySql没有mySql odbc驱动器 需要安装 MySql ODBC驱动器.百度下载mysql-connector-odbc-5.3.4-wi ...
- Linux学习记录--匿名沟通渠道
匿名沟通渠道 管道Linux最初支持Unix IPC其中的一种形式.具有下列特征: 1.管道是半双工.数据可以仅在一个方向流动:当双方需要沟通.建设两条管线需要. 2.仅仅能用于父子进程或者兄弟进程之 ...
- Eclipse背景和匹配出现单词的一些设置
Eclipse的背景色和关键词的设置这里就不多说了,只说明设置路径: 背景色:[Window]--->[Preference]-->[General]--->[Editors]--- ...