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) 额外空间的条件下 ...
随机推荐
- TaskFactory单例模式利用xml
/** * * Copyright (c) 1995-2009 Wonders Information Co.,Ltd. * 1518 Lianhang Rd,Shanghai 201112.P.R. ...
- Polly+AspectCore实现熔断与降级机制
Polly+AspectCore实现熔断与降级机制 https://www.cnblogs.com/edisonchou/p/9159644.html 一.熔断.降级与AOP 1.1 啥是熔断? 在广 ...
- scrapy-redis 分布式哔哩哔哩网站用户爬虫
scrapy里面,对每次请求的url都有一个指纹,这个指纹就是判断url是否被请求过的.默认是开启指纹即一个URL请求一次.如果我们使用分布式在多台机上面爬取数据,为了让爬虫的数据不重复,我们也需要一 ...
- Vsftp设置为PASV mode(被动模式传送)
首先配置vsftpd.conf文件: #vi /etc/vsftpd/vsftpd.conf 在文件的末尾加上: pasv_enable=YES pasv_max_port=30010 pasv_mi ...
- Linux下软件安装的四种方式
一.源码安装 步骤: 下载,解压源码(常见的源码打包格式:.tar.gz/.tar.bz2); 可以直接下载源码再上传至linux服务器,或者在联网状态下,直接通过wget等命令获取源码安装包;源码解 ...
- java 通过文件后缀名查找文件
最近开发项目的时候需要过滤出一些指定的文件,所以有了以下的一些代码: /** **该类主要是过滤得到指定后缀名的文件 **/ public class DataFileFilter implement ...
- nginx只允许域名访问网址,禁止ip访问
修改nginx配置 文件 在server段里插入如下正则: if ( $host != 'www.baidu.com') { return 403; } 说明:如果访问讨还不是www.baidu.co ...
- 洛谷 P1588 丢失的牛
题目描述 FJ丢失了他的一头牛,他决定追回他的牛.已知FJ和牛在一条直线上,初始位置分别为x和y,假定牛在原地不动.FJ的行走方式很特别:他每一次可以前进一步.后退一步或者直接走到2*x的位置.计算他 ...
- (转)在SQL Server 2016,Visual Studio 2017环境下,连接数据库屡屡失败,在connectionString上出的问题
适用情景: 1,ServerVersion出了问题,“SqlCnt.ServerVersion”引发了类型“System.InvalidOperationException”的异常 2,在String ...
- Spark源码分析之-Storage模块
原文链接:http://jerryshao.me/architecture/2013/10/08/spark-storage-module-analysis/ Background 前段时间琐事颇多, ...