leetcode 21 list merge
/**
* 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 *l3,*head;
if(l1->val<l2->val) {
l3=l1;
l1=l1->next;
}
else {
l3=l2;
l2=l2->next;
}
head=l3;
while(l1!=NULL&&l2!=NULL) {
if(l1->val<=l2->val) {
l3->next=l1;
l1=l1->next;
l3=l3->next;
}
else {
l3->next=l2;
l2=l2->next;
l3=l3->next;
}
}
while(l1!=NULL) {
l3->next=l1;
l1=l1->next;
l3=l3->next;
}
while(l2!=NULL) {
l3->next=l2;
l2=l2->next;
l3=l3->next;
}
return head;
}
};
leetcode 21 list merge的更多相关文章
- LeetCode(21)题解:Merge Two Sorted Lists
https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked lists and return it as ...
- (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] 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)
21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode ...
- 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 合并有序链表
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
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- Java [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 spli ...
随机推荐
- Hadoop集群批量命令执行
./pdsh -R ssh -w node-10-0[0-5] hostname -R:指定传输方式,默认为rsh,本例为ssh,如果希望ssh传输需要另行安装pdsh-rcmd-ssh,如果希望ss ...
- display:inline-block解决文字有间隙问题
定义:display:inline-block是使元素以块级元素的形式呈现在行内.意思就是说,让这个元素显示在同一行不换行,但是又可以控制高度和宽度,这相当于内联元素的增强. 但是display:in ...
- nginx入门学习步骤(linux)
一.nginx下载(nginx-1.9.9) http://nginx.org/download/ 二.解压到指定文件夹 tar -zxvf 解压缩文件 三.设置配置信息 在nignx解压文件夹内执行 ...
- 1048: [HAOI2007]分割矩阵
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1184 Solved: 863[Submit][Status][Discuss] Descripti ...
- 2- vue django restful framework 打造生鲜超市 -环境搭建
使用Python3.6与Django2.0.2(Django-rest-framework)以及前端vue开发的前后端分离的商城网站 项目支持支付宝支付(暂不支持微信支付),支持手机短信验证码注册, ...
- DevOps - 监控告警 - Zabbix
官网3.4版本中文文档 Zabbix documentation in Chinese [Zabbix Documentation 3.4] https://www.zabbix.com/docume ...
- h5获取摄像头拍照功能
完整代码展示 <!DOCTYPE html> <head> <title>HTML5 GetUserMedia Demo</title> <met ...
- 记忆化搜索:HDU1078-FatMouse and Cheese(记忆化搜索)
FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Leetcode 872. 叶子相似的树
题目链接 https://leetcode-cn.com/problems/leaf-similar-trees/description/ 题目描述 请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到 ...
- mac terminal基本命令
文件目录 首先要清楚几个文件目录: " / " :根目录 " ~ " :用户主目录的缩写.例如当前用户为esther,那么" ~ "展开来 ...