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. 解 ...
随机推荐
- android5.0(Lollipop) BLE Central牛刀小试
转载请表明作者:http://blog.csdn.net/lansefeiyang08/article/details/46482073 昨天写了android L BLE Peripheral的简单 ...
- iframe间的通信
父框架 <body></body> <script type="text/javascript"> document.domain = '100 ...
- 14.19 InnoDB and MySQL Replication InnoDB 和MySQL 复制:
14.19 InnoDB and MySQL Replication InnoDB 和MySQL 复制: MySQL 复制工作对于InnoDB 表和对于MyISAM表. 它是可能使用复制的方式 存储引 ...
- Effective C++_笔记_条款01_视C++为一个语言联邦
(整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) C++的各种能力和特性使它成为一个无可匹敌的工具,但也可能引发某 ...
- oracle触发器中增删改查本表
oracle触发器中增删改查本表 (1)只有before insert触发器中才可以查询或更新本表 create or replace trigger tri_test_ins before inse ...
- OO alv report
DATA: gr_alvgrid TYPE REF TO cl_gui_alv_grid ,"ALV对象 gt_fieldcat TYPE lvc_t_fcat , "ALV字段控 ...
- 积累的VC编程小技巧之工具提示
1.用鼠标移动基于对话框的无标题栏程序的简单方法 void CVCTestDlg::OnLButtonDown(UINT nFlags, CPoint point) { //一句话解决问题 ...
- vc 制作图片资源dll
方法一: 使用纯WIN32 DLL方法封装纯资源第一步,通过VS2005建立WIN32 DLL 空工程第二步,设置配置属性->链接器->高级->无入口点(是/NOENTRY)设置配置 ...
- Java实现定时任务的三种方法(转)
在应用里经常都有用到在后台跑定时任务的需求.举个例子,比如需要在服务后台跑一个定时任务来进行非实时计算,清除临时数据.文件等.在本文里,我会给大家介绍3种不同的实现方法: 普通thread实现 Tim ...
- Main Memory Object-Relational Database Management System
Main Memory Object-Relational Database Management System FastDBMain Memory Relational Database Manag ...