leetcode-26-exercise_linked-list
141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.
解题思路:
需要检查before和after隔一个的情况。因为除了开始时,如果检查的是before和after相邻,那么两个元素成环时,after跑到before后面,也就
不能检查到环了。
bool hasCycle(ListNode *head) {
if (head == NULL)
return false;
ListNode* before = head;
ListNode* after = before->next;
while (after != NULL && after->next != NULL) {
if (after == before)
return true;
before = before->next;
after = after->next->next;
}
return false;
}
21. Merge Two Sorted Lists
按升序合并两个链表
解题思路:
当l1和l2不空时,比较它们的表头值,放入小的,同时移动指针。当有一个链表为空时,剩下的那个必然是更大的部分,直接连接上就好。
最后,由于初始化时给了一个表头,所以要返回表头下一个点。
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* result = new ListNode(0);
ListNode* temp = result;
while (l1 != NULL && l2 != NULL) {
if (l1->val < l2->val) {
temp->next = l1;
l1 = l1->next;
} else {
temp->next = l2;
l2 = l2->next;
}
temp = temp->next;
}
if (l1 != NULL)
temp->next = l1;
else
temp->next = l2;
return result->next;
}
leetcode-26-exercise_linked-list的更多相关文章
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- 前端与算法 leetcode 26. 删除排序数组中的重复项
目录 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 26. 删除排序数 ...
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- Java实现 LeetCode 26 删除排序数组中的重复项
26. 删除排序数组中的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) ...
- leetcode 26
26. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- LeetCode 26 Remove Duplicates from Sorted Array (移除有序数组中重复数字)
题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重 ...
- LeetCode(26)题解:Remove Duplicates from Sorted Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...
- [LeetCode]26. 删除排序数组中的重复项(数组,双指针)
题目 给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下 ...
随机推荐
- atom通过remote ftp同步本地文件到远程主机的方法
视频教程:https://ninghao.net/video/3991 搜索 “remote ftp”, 点击 “Package”搜索包,Install”安装 本地打开需要同步的项目目录 创建 rem ...
- Net Core -- 配置Kestrel端口
Net Core -- 配置Kestrel端口 Kestrel介绍 在Asp.Net Core中,我们的web application 其实是运行在Kestrel服务上,它是一个基于libuv开源的跨 ...
- HDU 1114 Piggy-Bank 完全背包 dp
http://acm.hdu.edu.cn/showproblem.php?pid=1114 完全背包的题目,要求输出最小价值.然后一定要把给出的背包重量全部用完. 就是问一个背包为k的大小,n件物品 ...
- 开启 PHP 错误提示配置步骤详解
PHP编码出错不提示,这对于开发来说,是很不方便的.下面讲解如何开启错误提示步骤: 1. 打开php.ini文件. 以我的ubuntu为例,这个文件在: /etc/php5/apache2 目录下. ...
- 优化vue-cli构建的文件体积
vue-router 懒加载优化 先安装 babel 动态引入插件 npm install --save-dev babel-plugin-syntax-dynamic-import 修改router ...
- Collections集合工具类,集合嵌套,集合综合案例斗地主
1 Collections集合工具类 (可以对比Arrays工具类共同记忆) 常用方法: 例: import java.util.ArrayList; import java.util.Collect ...
- springboot集成shiro实现验证码校验
github:https://github.com/peterowang/shiro/ 这里实现验证码校验的思路是自己添加一个Filter继承FormAuthenticationFilter,Form ...
- go语言简单的soap调用方法
package main import ( "bytes" "encoding/xml" "fmt" "io" &quo ...
- deb软件安装
deb是debian linux的安装格式,跟red hat的rpm非常相似,最基本的安装命令是:dpkg -i file.deb dpkg 是Debian Package的简写,是为Debian 专 ...
- Class 类
在javascript 中应用类的概念 // javascript web applications 富应用开发 // 类库:生成类的地方:给所有的构造函数提供基础方法,如 extend, inclu ...