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. 解 ...
随机推荐
- HTML - HTML Commonly Used Character Entities
HTML Entities Some characters are reserved in HTML. It is not possible to use the less than (<) o ...
- CSU1664: 防水堤坝
Description 在太平洋的一个小岛上,岛民想要建立一个环岛的堤坝,我们能够将小岛简化为一个二维平面,你须要使用K条边(这些边要么是水平或者垂直长度为1的边,要么是45度倾斜的长度为√2的边)围 ...
- 浅谈Jquery的使用下篇
上一篇我们已经探讨了Jquery的有关的内容,简单的选择器以及一些Jquery的属性事件和方法等内容,让我们简单的学到了Jquery的比较基础的内容,下面我们就来探讨Jquery的一些其它的内容,比如 ...
- 鼠标进入与离开的消息(使用CM_MOUSEENTER来判断是否进入控件)
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs ...
- HDU 4839 The Game of Coins _(:зゝ∠)_
The Game of Coins mark: #include"cstdio" #include"iostream" #include"queue& ...
- Spark大型项目实战:电商用户行为分析大数据平台
本项目主要讲解了一套应用于互联网电商企业中,使用Java.Spark等技术开发的大数据统计分析平台,对电商网站的各种用户行为(访问行为.页面跳转行为.购物行为.广告点击行为等)进行复杂的分析.用统计分 ...
- 虚拟机安装中文Fedora14和C/C++IDE开发环境
虚拟机安装中文Fedora14和C/C++IDE开发环境 2010-12-05 00:15:58 标签:中文Fedora14 IDE 开发环境 C/C++ 原创作品,允许转载,转载时请务必以超链接形式 ...
- 关于wind7重新安装系统后,连接mysql的问题
系统重装对于我们来说,可以说是家常便饭了.但重装系统之后 ,又要装很多的应用软件是会很烦的.特别是重装一些数据库软件,时间长不说,搞不好数据丢失了会让人抓狂. 今天我简单介绍一个不用重装mysql的方 ...
- hdu 4455 Substrings (DP 预处理思路)
Substrings Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- BAPI总的数据库提交和回滚
BAPI事物中的数据提交和回滚必须通过调用SAP标准业务对象BAPI SERVICE(对象类型SAP0001)的BAPI方法bapiservic.transactioncommit和bapiservi ...