141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos
which represents the position (0-indexed) in the linked list where tail connects to. If pos
is -1
, then there is no cycle in the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0 Output: true Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1 Output: false Explanation: There is no cycle in the linked list.
分析:
有两种解决办法:
1,采用一个set集合,遍历listnode,如果没有包含,就add,如果有,就说明曾经遍历过,返回true
时间复杂度o(n)
空间复杂度o(n)
代码:
public static boolean hasCycle(ListNode head){ Set<ListNode> nodes=new HashSet<>(); while(head!=null){ if(nodes.contains(head)){ return true; }else{ nodes.add(head); } head=head.next; } return false; }
2,采用龟兔赛跑的概念,乌龟一次跑一步,兔子一次跑两步,如果乌龟和兔子遇见过,说明存在环,如果没有,则表示没有
时间复杂度o(n)
空间复杂度o(1)
代码如下:
public static boolean hasCycle(ListNode head){ if(head.next==null) return false; ListNode first = head; // 乌龟,一次跳一个 ListNode second = head; // 兔子,一次跳两个 while(second!=null&&second.next!=null){ first=first.next; second=second.next.next; if (first==second){ return true; } } return false; }
相关推荐
javascript js_leetcode题解之142-linked-list-cycle-ii.js
javascript js_leetcode题解之141-linked-list-cycle.js
python python_leetcode题解之142_Linked_List_Cycle_II
python python_leetcode题解之141_Linked_List_Cycle
leetcode 不会 Leetcode Solutions in Java Linked List Linked List Cycle Given a linked list, determine if it has a cycle in it. public static boolean hasCycle(ListNode head) 快慢指针法,块指针从head....
leetcode 答案leetcode-java leetcode.com 的 Java 答案 ================索引================ com.leetcode.array Search a 2D Matrix Spiral Matrix com.leetcode.list Linked List Cycle Linked List Cycle II ...
linked-list-cycle-ii 链表 linked-list-cycle 链表 copy-list-with-random-pointer 复杂度 single-number 动态规划 candy 贪心 gas-station 动态规划 palindrome-partitioning-ii 动态规划 triangle 树 sum-root-to...
leetcode怎么销号 LeetCode便签 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? 解决思路 声明一个慢指针和一个快...
142-环形链表:linked-list-cycle-ii 160-相交链表:intersection-of-two-linked-lists 206-反转一个单链表:reverse-linked-list 20-有效的括号:valid-parentheses 150-逆波兰表达式求值:evaluate-reverse-polish-...
leetcode中325题python leetcode 以 参考 和 Hash相关 1_两数之和 ...linked-list-cycle-ii 143 重排链表 reorder-list 148 排序链表 sort-list 234 回文链表 palindrome-linked-list 双指针遍历/滑动
141.Linked-List-Cycle │ ├── solution.cpp │ ├── solution.go │ └── solution.md [ -> Explain thoughts about this problem.] ├── 404.Sum-of-Left-Leaves │ ├── solution.cpp │ ├...
Cycle trees Convert Sorted Array to Binary Search Tree string and search First Bad Version Dynamic Programing *** Climbing Stairs Set Matrix Zeroes API System.arrayCopy 刷题顺序 TOP100 其中算法,主要...
Linked List Cycle**:判断链表是否有环,如有,找到环的入口节点。 - **19. Remove Nth From End**:移除链表中的第 n 个节点。 - **206. Reverse Linked List**:反转整个链表。 - **21. Merge Two Sorted ...
preorder-traversal链表reorder-list链表linked-list-cycle-ii链表linked-list-cycle动态规划word-break-ii动态规划word-break链表copy-list-with-random-pointer复杂度single-number-ii复杂度single-number动态规划
leetcode leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring Without Repeating Characters 5.Longest Palindromic Substring 20.Valid Parentheses 26.Remove Duplicates from Sorted ...
leetcode中国 Leetcode Set Matrix Zeroes 第一种 通过一次遍历记录所有为0的索引(Python中enumerate()输出当前列表的索引) 再遍历一次, 根据记录的索引进行置0 第二种 通过一次遍历所有为0的索引, 设置当前索引的...
* [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...
Linked List Cycle 判断链表是否有环。通过快慢节点可以简单实现。 Unique Binary Search Trees 本题参考了 里面的*How Many Binary Trees Are There?*章节。 The first few terms: C(0) = 1 C(1) = C(0)C(0) = 1 C...
leetcode 答案Leet Code 挑战 这是我提交给 Lambda School CS Unit 2 构建周的已接受 ...如果您想自己尝试,这些链接将带您进入代码挑战。...要查看我接受的答案,只需转到与...[Linked List Cycle II](https://leetcode.co
linked-list-cycle/Solution.993783.java $ tree . ├── add-binary │ └── Solution.665166.java ├── add-two-numbers │ └── Solution.666385.java ├── balanced-binary-tree │ └── ...