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)相关推荐

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

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

  3. 【leetcode】Remove Linked List Elements(easy)

    Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 -- ...

  4. 【LeetCode】50. Pow(x, n) (3 solutions)

    Pow(x, n) Implement pow(x, n). 按照定义做的O(n)肯定是TLE的. 利用这个信息:x2n = (xn)2 有个注意点,当n为负是,直接取反是不可行的. 由于int的表示 ...

  5. 【leetcode】解题日记(未完待续)

    开坑,有生之年系列,希望有一天能解出 leetcodeleetcodeleetcode 上的所有题目. 写题解好麻烦,懒得写(手动狗头),进度如下,不定期更新. 总题数 已解答 题解数 2058 23 ...

  6. 【LeetCode】【HOT】141. 环形链表(快慢指针)

    [LeetCode][HOT]141. 环形链表 文章目录 [LeetCode][HOT]141. 环形链表 package hot;class ListNode{int val;ListNode n ...

  7. 【leetcode】86. Partition List

    题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

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

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  10. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

最新文章

  1. php ci 调用控制器,如何从Codeigniter中的另一个控制器加载一个控制器?
  2. java 初级编程题_java基础经典编程题
  3. m.2接口和nvme区别_m.2 nvme和m.2有区别吗?
  4. [BJDCTF2020]Cookie is so stable
  5. 调用newtonsoft.json反序列出错
  6. 转载 3年工作经验 面试大厂
  7. 调用图像_本地图像的保存和调用
  8. 数据库分库分表中间件 Sharding-JDBC 源码分析 —— SQL 路由(二)之分库分表路由...
  9. 计算机网络超详细笔记(五):网络层
  10. 绝对值编码器:从调研到开发
  11. python 移动文件 使用os.rename
  12. 全球十大管理咨询公司
  13. ux设计_我是一名开发人员,正在过渡到UX设计帮助
  14. UpdateData()函数的用法
  15. shader拖尾_Unity LineRenderer 之 鼠标轨迹记录和拖尾实现
  16. APK包名修改 请问如何修改APK包名
  17. Vuejs和Javascript中的this变量的理解
  18. 北京Java培训出来,Java程序员工资一般能拿多少?
  19. List里的remove方法
  20. IDEA如何创建webapp(IDEA)

热门文章

  1. vue+vue-cli2+webpack配置资源cdn
  2. POJ-3273(二分)
  3. 项目管理:文档可测试化
  4. 【mybatis】学习笔记之conf.xml与mapper.xml配置
  5. 宽字节UTF-8、多字节互转
  6. Django 输出二维码
  7. 详解java的构造方法
  8. request.getRequestDispatcher(url).forward(request, response)
  9. oc 把view添加到rootcontrollerview控制的view
  10. hdu 2550 百步穿杨(大水题)