【LeetCode】141. Linked List Cycle (2 solutions)
Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
解法一:
使用unordered_map记录当前节点是否被访问过,如访问过说明有环,如到达尾部说明无环。
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:bool hasCycle(ListNode *head) {unordered_map<ListNode*, bool> visited;while(head != NULL){if(visited[head] == true)return true;visited[head] = true;head = head->next;}return false;} };
解法二:不使用额外空间
设置快慢指针,
fast每次前进两步,slow每次前进一步,如相遇说明有环,如到达尾部说明无环。
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:bool hasCycle(ListNode *head) {ListNode* fast = head;ListNode* slow = head;do{if(fast != NULL)fast = fast->next;elsereturn false;if(fast != NULL)fast = fast->next;elsereturn false;slow = slow->next;}while(fast != slow);return true;} };
【LeetCode】141. Linked List Cycle (2 solutions)相关推荐
- 【LeetCode】142 - Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [Leetcode]141. Linked List Cycle
题目: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin ...
- 【leetcode】Remove Linked List Elements(easy)
Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...
- 【LeetCode】50. Pow(x, n) (3 solutions)
Pow(x, n) Implement pow(x, n). 按照定义做的O(n)肯定是TLE的. 利用这个信息:x2n = (xn)2 有个注意点,当n为负是,直接取反是不可行的. 由于int的表示 ...
- 【leetcode】解题日记(未完待续)
开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...
- 【LeetCode】【HOT】141. 环形链表(快慢指针)
[LeetCode][HOT]141. 环形链表 文章目录 [LeetCode][HOT]141. 环形链表 package hot;class ListNode{int val;ListNode n ...
- 【leetcode】86. Partition List
题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...
- 【leetcode】109. Convert Sorted List to Binary Search Tree
题目如下: Given a singly linked list where elements are sorted in ascending order, convert it to a heigh ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】100. 相同的树
题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...
最新文章
- php ci 调用控制器,如何从Codeigniter中的另一个控制器加载一个控制器?
- java 初级编程题_java基础经典编程题
- m.2接口和nvme区别_m.2 nvme和m.2有区别吗?
- [BJDCTF2020]Cookie is so stable
- 调用newtonsoft.json反序列出错
- 转载 3年工作经验 面试大厂
- 调用图像_本地图像的保存和调用
- 数据库分库分表中间件 Sharding-JDBC 源码分析 —— SQL 路由(二)之分库分表路由...
- 计算机网络超详细笔记(五):网络层
- 绝对值编码器:从调研到开发
- python 移动文件 使用os.rename
- 全球十大管理咨询公司
- ux设计_我是一名开发人员,正在过渡到UX设计帮助
- UpdateData()函数的用法
- shader拖尾_Unity LineRenderer 之 鼠标轨迹记录和拖尾实现
- APK包名修改 请问如何修改APK包名
- Vuejs和Javascript中的this变量的理解
- 北京Java培训出来,Java程序员工资一般能拿多少?
- List里的remove方法
- IDEA如何创建webapp(IDEA)
热门文章
- vue+vue-cli2+webpack配置资源cdn
- POJ-3273(二分)
- 项目管理:文档可测试化
- 【mybatis】学习笔记之conf.xml与mapper.xml配置
- 宽字节UTF-8、多字节互转
- Django 输出二维码
- 详解java的构造方法
- request.getRequestDispatcher(url).forward(request, response)
- oc 把view添加到rootcontrollerview控制的view
- hdu 2550 百步穿杨(大水题)