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 ...
随机推荐
- ROS模拟
亲测,在古月大大这篇博客中的一条命令最好改为rostopic pub /cmd_vel geometry/Twist -r 10 -- '[0.2,0,0]' '[0,0,0.5]'. http:// ...
- Mac 中使用phpstorm 修改文件提示"only read",只读权限
在终端中执行命令: 给max系统用户liutao赋予整个项目文件权限,即可成功 sudo chown -R liutao /Users/liutao/Desktop/vagrant/newprojec ...
- leetcode 143. Reorder List 、86. Partition List
143. Reorder List https://www.cnblogs.com/grandyang/p/4254860.html 先将list的前半段和后半段分开,然后后半段进行逆序,然后再连接 ...
- ifc构件加载到树形控件中
void IfcTreeWidget::setParentCheckState(QTreeWidgetItem *item) { if(!item) return; ; int childCount ...
- 阶段5 3.微服务项目【学成在线】_day18 用户授权_18-微服务之间认证-需求分析
4.1 需求分析 前边章节已经实现了用户携带身份令牌和JWT令牌访问微服务,微服务获取jwt并完成授权. 当微服务访问微服务,此时如果没有携带JWT则微服务会在授权时报错. 测试课程预览: 1.将课程 ...
- 使用Async-profiler 对程序性能优化实战
原文在简书上, https://www.jianshu.com/p/f8336b835978 1.背景 目前有一个kafka消费者工程,此工程会消费kafka中的消息,并通过fastjson解析该消息 ...
- 123456123456#0#-----com.threeapp.xiongMaoPaoPao01----熊猫跑酷01
com.threeapp.xiongMaoPaoPao01----熊猫跑酷01
- Tools - Nmap
Nmap Homepage Nmap参考指南(Man Page) Nmap中文网 wiki - Nmap 常用示例 1) Ping扫描,打印出对扫描做出响应的主机 nmap -sP 192.168.1 ...
- iOS笔试题03
1. When to use NSMutableArray and when to use NSArray? 1> 当数组元素需要动态地添加或者删除时,用NSMutableArray 2> ...
- Vue + GraphQL初试
基本用法 GraphQL概述 GraphQL基本语法特性 GraphQL类型系统 GraphQL类型系统内置基础类型 GraphQL类型系统内置修饰符 GraphQL工作原理 GraphQL执行过程 ...