LeetCode(21)题解:Merge Two Sorted Lists
https://leetcode.com/problems/merge-two-sorted-lists/
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.
思路:
考察链表操作,没啥说的。
AC代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *p=new ListNode();
ListNode *q=p;
while(l1!=NULL && l2!=NULL){
if(l1->val<l2->val){
q->next=l1;
q=q->next;
l1=l1->next;
}
else{
q->next=l2;
q=q->next;
l2=l2->next;
}
}
if(l1!=NULL)
q->next=l1;
else
q->next=l2;
p=p->next;
return p;
}
};
LeetCode(21)题解:Merge Two Sorted Lists的更多相关文章
- (LeetCode 21)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 ...
- 【LeetCode算法-21】Merge Two Sorted Lists
LeetCode第21题 Merge two sorted linked lists and return it as a new list. The new list should be made ...
- 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
- [Leetcode][Python]23: Merge k Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...
- LeetCode LinkList 23. Merge k Sorted Lists
这两天一直也没有顾上记录一下自己做过的题目,回头看看,感觉忘的好快,今天做了一个hard,刚开始觉得挺难得,想了两种方法,一种是每次都从k个list中选取最小的一个,为空的直接跳过,再就是每次合并其中 ...
- 【LeetCode练习题】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 Two Sorted Lists(有序链表归并)
题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...
- [LeetCode 题解]: Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...
- LeetCode(21)Merge Two Sorted Lists
题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...
随机推荐
- java中传入一个数或字符串或数组进行反转
//将一个数用递归反转--利用余数 public static void inverse(int n) { System.out.print(n % 10); if (n >= 10) inve ...
- Luogu【P2065】贪心的果农(DP)
题目链接 几乎所有DP题目前本蒟蒻都没有思路.当然包括但不限于这道题.每次都是看了题解然后打的(等价于抄题解)很羞耻 这道题经思考发现,越靠前砍的果树长果子的能力一定越弱,如果长果子的能力一样弱就先把 ...
- 【Luogu】P1593因子和(唯一分解定理,约数和公式)
题目链接 首先介绍两个定理. 整数唯一分解定理:任意正整数都有且只有一种方式写出素数因子的乘积表达式. \(A=(p1k1 p2k2 ...... pnkn \) 求这些因子的代码如下 ;i*i< ...
- mysql5.7.23版本安装教程
亲身实践安装mysql,用时居然花费了三个小时,在有那么多教程的情况下,依然在不该花费时间的路上浪费了太多时间.希望这篇文章能够帮助大家少走弯路~~ 1.下载我下载的是64位. 2.解压下载之后,我选 ...
- [BZOJ4506] [Usaco2016 Jan]Fort Moo(DP?)
传送门 总之可以先预处理出来每个位置最多往上延伸多少 枚举两行,看看夹在这两行中间的列最大能构成多大的矩形 可以看出,必须得在一个两行都没有X的区间才有可能构成最大的答案 那么可以把这些区间处理出来, ...
- R语言入门--画图(一)--ggplot2
先写一些需要用到的知识点,比如包.函数 dplyr 很好用的包 经常与ggplot2连用 mutate:用于对数据框的列进行重新处理,或者用处理的结果添加新列 数据清洗: 1.na.omit() ...
- 系统进程的Watchdog
编写者:李文栋 /rayleeya http://rayleeya.iteye.com/blog/1963408 3.1 Watchdog简介 对于像笔者这样没玩过硬件的纯软程序员来说,第一次看到这个 ...
- BZOJ——1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
http://www.lydsy.com/JudgeOnline/problem.php?id=1607 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 2 ...
- Codeforces 959 E Mahmoud and Ehab and the xor-MST
Discription Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him ...
- Spring的IoC容器概述
以下内容引用自http://wiki.jikexueyuan.com/project/spring/ioc-containers.html: IoC容器 Spring容器是Spring框架的核心.容器 ...