题目描述

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:

  1. Input: 1->2->4, 1->3->4
  2. Output: 1->1->2->3->4->4

参考答案

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. class Solution {
  10. public:
  11. ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
  12. ListNode res(); // content
  13. ListNode* cur = &res; // pointer cur = taking addree of res
  14. // *content. &pointer->
  15.  
  16. while(l1&&l2){
  17. if(l1->val > l2 ->val){
  18. cur->next = l2;
  19. l2 = l2->next;
  20. }else{
  21. cur->next = l1;
  22. l1 = l1->next;
  23. }
  24. cur = cur->next;
  25. }
  26. cur->next = l2?l2:l1;
  27. return res.next;
  28. }
  29. };

答案解析

新建一个空节点,不是地址,而是内容,然后将该内容对应的地址,附给一个cur指针。

进行loop

返回的时候,因为对于struct而言,提取成员时,内容使用 点,指针使用 -> 。

LC 21. Merge Two Sorted Lists的更多相关文章

  1. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

  2. 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 ...

  3. 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 ...

  4. 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists

    21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...

  5. 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 ...

  6. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

  7. 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 ...

  8. [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 ...

  9. [leetcode] 21. Merge Two Sorted Lists (Easy)

    合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...

随机推荐

  1. Docker Dockerfile 定制镜像

    使用 Dockerfile 定制镜像  镜像的定制实际上就是定制每一层所添加的配置.文件.如果我们可以把每一层修改.安装.构建.操作的命令都写入一个脚本,用这个脚本来构建.定制镜像,那么无法重复的问题 ...

  2. android通用的UUID唯一标示符

    http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id 版权声明:本文为博主原创文章,未经博主允许 ...

  3. window.postMessage 跨窗口,跨iframe javascript 通信

    同源通信 执行它们的页面位于具有相同的协议(http/https),端口(80/443),主机(通常为域名) 时,这两个脚本才能相互通信 大多数情况下,网站就是内部的域名,所以是同源通信,可以相互访问 ...

  4. Vue——路由:登录状态的判断

    在搭建的系统中,最基本的登录都是必须的,结合Vue的路由,涉及最多的就是登录状态的判断.也就是说,如果一个组件要校验登录状态,则在用户初始进入时,就要去判断用户是否登录,这里的校验登录状态就是本篇的重 ...

  5. Linux | Vim使用

    Linux vi/vim 所有的 Unix Like 系统都会内建 vi 文书编辑器,其他的文书编辑器则不一定会存在. 但是目前我们使用比较多的是 vim 编辑器. vim 具有程序编辑的能力,可以主 ...

  6. 通AI启示录,从一篇数学物理基础论文说起 原创: 关注前沿科技 量子位 今天 允中 发自 凹非寺

    通AI启示录,从一篇数学物理基础论文说起 原创: 关注前沿科技 量子位 今天 允中 发自 凹非寺

  7. uboot的仓库在哪里?

    答: https://gitlab.denx.de/u-boot/u-boot# 1. 获取源码 git clone git@gitlab.denx.de:u-boot/u-boot.git Or g ...

  8. 简易的CRM系统案例之Struts2&Spring整合+Hibernate3+JSP+MySQL版本

    主要对上一篇Struts2&Spring整合的改造 简易的CRM系统案例之Struts2+Hibernate3+JSP+MySQL版本 src/bean.xml <beans xmlns ...

  9. starUML建模C++【逆向工程】

    1.下载starUML 2.打开starUML,选择default approach 3.添加 Profile,把C++添加进去 4.在右侧的工程上点右键—[C++]—-[Reverse Engine ...

  10. InfluxDB权限认证机制

    一.介绍 权限认证机制,顾名思义,就是对 InfluxDB 数据库添加权限访问控制,在默认情况下,InfluxDB 的权限认证机制是关闭的,也就是说所有用户都有所有权限. 老规矩,直接实践上手,下图是 ...