LC 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 together the nodes of the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
参考答案
/**
* 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 res(); // content
ListNode* cur = &res; // pointer cur = taking addree of res
// *content. &pointer-> while(l1&&l2){
if(l1->val > l2 ->val){
cur->next = l2;
l2 = l2->next;
}else{
cur->next = l1;
l1 = l1->next;
}
cur = cur->next;
}
cur->next = l2?l2:l1;
return res.next;
}
};
答案解析
新建一个空节点,不是地址,而是内容,然后将该内容对应的地址,附给一个cur指针。
进行loop
返回的时候,因为对于struct而言,提取成员时,内容使用 点,指针使用 -> 。
LC 21. Merge Two Sorted Lists的更多相关文章
- [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 ...
随机推荐
- 请写出一段python代码实现删除list里面的重复元素?
l1 = ['b','c','d','c','a','a'] l2 = list(set(l1)) print(l2)
- gitlab怎么用
0101在个人资料里面去设置去找密钥.... 0102 点击生成密钥 0103 在文件夹的命令行输入 ssh-keygen -t rsa -C "your.email@example.com ...
- 深度解读Facebook刚开源的beringei时序数据库——数据压缩delta of delta+充分利用内存以提高性能
转自:https://yq.aliyun.com/topic/58?spm=5176.100239.blogcont69354.9.MLtp4T 摘要: Facebook最近开源了beringei时序 ...
- Redis学习(一) —— 基本使用与原理
一.数据结构 string Redis字符串是可修改字符串,在内存中以字节数组形式存在. 下面是string在源码中的定义,SDS(Simple Dynamic String) struct SDS& ...
- CV_Learn
CV学习进度条记录,也是SRTP的进度记录. 阶段一(2019.4-2019.5): 1.opencv简单操作学习.(实现了一些基础操作,从颜色通道到边缘轮廓,2019.4.22完成) 2.linux ...
- springmvc配置mybatis与hibernate的不同点
相信每个人对springmvc+hibernate或者springmvc+mybatis都不会陌生,拿来一个项目也都会开发.但是自己配置的情况下却很少,即使自己配置过,长时间不写也会忘,在这里记录一下 ...
- Java工作笔记:工作中使用JNA调用C++库的一些细节(转载)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/zjutzmh/article/detai ...
- Centos7.4服务器安装Laravel5.7详细讲解(2018-10-27)
一.在阿里云或者腾讯云选择Centos7并购买服务器 二.安装宝塔面板和php运行环境 1.输入命令 yum install -y wget && wget -O install.sh ...
- osgViewer::View::setUpViewOnSingleScreen()
void ViewerBase::frame(double simulationTime) { if (_done) return; // OSG_NOTICE<<std::endl< ...
- iOS UITextField设置placeholder颜色
设置UITextField的placeholder颜色 UIColor *color = [UIColor blackColor]; textField.attributedPlaceholder = ...