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 ...
随机推荐
- jmeter+ant执行 报错:Reference xslt.classpath not found 【采坑记录】
问题: report: BUILD FAILED E:\jmeter\apache-jmeter-4.0\testcase\build.xml:29: The following error occu ...
- zabbix(4)数据库表分区优化
一.zabbix 数据库存储 zabbix-server将采集到的数据存储在数据库(mysql.oracle等),而数据存储的大小与每秒处理的数量量有关,因此数据存储取决于以下两个因数: (1)Req ...
- 如何利用awk累加第一列的值?
以下是一个五行文件的例子: 1.[root@master yjt]# seq 5 |awk 'BEGIN{sum=0;print "总和:"}{if(NR<=4)printf ...
- c标签页面进行解析json
JAVA代码中的后台 List<Map<String,String>> rs = new ArrayList<Map<String,String>>() ...
- linux 的real time 、user time、 sys time
<APUE>上提到了三个时间Real time, User time和Sys time.这三者是什么关系呢?在使用time(1)命令的时候,为什么real time < user t ...
- 读取文件名.cpp
#include <io.h> #include <iostream> #include <string> #include <windows.h> # ...
- 最简单的SpringAop 小案例
网盘下载地址: 链接:https://pan.baidu.com/s/1Z-em-1ouWyXeMP3JW0IbCg 提取码:0o4o 1.目录结构: 2.配置文件 applicationCo ...
- 灵活使用ssh、dsh和pssh高效管理大量计算机
http://os.iyunv.com/art/201012/240113.htm 灵活使用ssh.dsh和pssh高效管理大量计算机 http://os.iyunv.com2010-12-23 09 ...
- usb 设备 复位
How to Reset USB Device in Linux by ROMAN10 on MAY 4, 2011 · 9 COMMENTS USB devices are anywhere n ...
- insmod某个内核模块时提示“Failed to find the folder holding the modules”如何处理?
答: 创建/lib/modules/$(uname -r)目录,命令如下: mkdir /lib/modules/$(uname -r)