21.Merge Two Sorted Lists (List)
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.
/**
* 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) {
if(l1 == NULL) return l2;
if(l2 == NULL) return l1; ListNode* root = l1;
if(l1->val <= l2->val){
root = l1;
l1 = l1->next;
}
else{
root = l2;
l2 = l2->next;
} ListNode* current = root;
while(l1 && l2){
if(l1->val <= l2->val){
current->next = l1;
l1 = l1->next;
}
else{
current->next = l2;
l2 = l2->next;
}
current = current->next;
} if(l1){
current->next = l1;
}
else if(l2){
current->next = l2;
} return root;
}
};
21.Merge Two Sorted Lists (List)的更多相关文章
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- 21. Merge Two Sorted Lists(合并2个有序链表)
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
- leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- 刷题21. Merge Two Sorted Lists
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- C# 写 LeetCode easy #21 Merge Two Sorted Lists
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- [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 (Easy)
合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...
- [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 ...
随机推荐
- C# #if, #else和#endif预处理指令
#if 使您可以开始条件指令,测试一个或多个符号以查看它们是否计算为 true.如果它们的计算结果确实为true,则编译器将计算位于 #if 与最近的 #endif 指令之间的所有代码.例如, ...
- 博客(第0次作业)—— New Starting Point
一.最理想的师生关系是健身教练和学员的关系,在这种关系中你期望获得来自老师的那些帮助? 正如文章中所说,这些学员的想法得足够强烈, 他/她才会花钱去参加这样的健身活动,每一个来学习的学生, 都是想学 ...
- ubuntu 通过ssh上传/下载服务器文件
1.用ssh登录远程ubuntu主机 (主机ip为:1.2.3.4;用户名:username) ssh username@1.2.3.4 2.从远程ubuntu主机copy文件/文件夹到本地(scp) ...
- IDEA 使用generator逆向工程生成pojo,mapper
1.新建立一个MAVEN项目 2.在pom.xml增加配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns: ...
- flow 编写flow-typed 定义(官方文档)
此为官方文档,因为墙的问题,记录下来: Before spending the time to write your own libdef, we recommend that you look to ...
- celery docker 基本使用
项目参考官网资料,比较简单的add task 具体代码参考https://github.com/rongfengliang/celery-docker-demo 项目结构 ├── README.md ...
- eclipse中点不出来提示
当在用eclipse或是myeclipse时,可能会遇到不能自动提示,就是当你用到点的时候,后面不会出现相关的提示信息.这时,解决方法如下 : 1.菜单window->Preferences-& ...
- stacks and queues--codility
lesson 7: stacks and queues 1. Nesting 2. StoneWall 3. Brackets 4. Finsh lesson 7: stacks and queues ...
- wamp 配置多站点访问
1:在f:\wamp\bin\apache\apache2.2.21\conf目录下打开 httpd.conf 查找到 #include conf/extra/httpd-vhosts.conf 把前 ...
- Java 字符串与对象进行比较 compareTo()
Java 手册 compareTo public int compareTo(String anotherString) 按字典顺序比较两个字符串.该比较基于字符串中各个字符的 Unicode 值.按 ...