博客
关于我
LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
阅读量:534 次
发布时间:2019-03-08

本文共 905 字,大约阅读时间需要 3 分钟。

LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

目录


前言

数据结构的内容很细,需要翻书阅读。

题目

今天的题目是关于链表的,给定两个链表,然后合并这两个链表,要求从小到大。

语言

C++

思路

其实这种合并两个链表的在书上(数据结构第五版,李春葆,68页)就有详细的案例,采用的是二路合并法。

源码

class Solution {public:    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {        ListNode* head = NULL;        if(l1 == NULL)             return l2;        if(l2 == NULL)             return l1;        if(l1->val > l2->val){            head = l2;            l2 = l2->next;        }        else{            head = l1;            l1 = l1->next;        }           ListNode* p = head;         while(l1 && l2){            if(l1->val > l2->val){                p->next = l2;                l2 = l2->next;            }            else{                p->next = l1;                l1 = l1->next;            }            p = p->next;        }        p->next = l1 ? l1 : l2;        return head;            }};

后记

每日一刷再忙也得坚持,相信会有收获的。

转载地址:http://soyiz.baihongyu.com/

你可能感兴趣的文章
nodejs 的 Buffer 详解
查看>>
NodeJS 的环境变量: 开发环境vs生产环境
查看>>
nodejs 读取xlsx文件内容
查看>>
nodejs 运行CMD命令
查看>>
Nodejs+Express+Mysql实现简单用户管理增删改查
查看>>
nodejs+nginx获取真实ip
查看>>
nodejs-mime类型
查看>>
NodeJs——(11)控制权转移next
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
NodeJS、NPM安装配置步骤(windows版本)
查看>>
nodejs下的express安装
查看>>
nodejs与javascript中的aes加密
查看>>
nodejs中Express 路由统一设置缓存的小技巧
查看>>
nodejs中express的使用
查看>>
Nodejs中搭建一个静态Web服务器,通过读取文件获取响应类型
查看>>
Nodejs中的fs模块的使用
查看>>
NodeJS使用淘宝npm镜像站的各种姿势
查看>>
NodeJs入门知识
查看>>
nodejs包管理工具对比:npm、Yarn、cnpm、npx
查看>>
NodeJs单元测试之 API性能测试
查看>>