- 浏览: 183547 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
判断一个链表中是否有环存在,用快慢指针来解决,设定两个指针fast和slow都指向head,我们让fast = fast.next.next,slow = slow.next,如果存在环,fast和slow就会相遇,因此当fast == slow时我们就返回true, 否则返回false。代码如下:
Follow up:
Can you solve it without using extra space?
判断一个链表中是否有环存在,用快慢指针来解决,设定两个指针fast和slow都指向head,我们让fast = fast.next.next,slow = slow.next,如果存在环,fast和slow就会相遇,因此当fast == slow时我们就返回true, 否则返回false。代码如下:
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { if(head == null || head.next == null) return false; ListNode fast = head; ListNode slow = head; while(fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if(slow == fast) return true; } return false; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
python python_leetcode题解之141_Linked_List_Cycle
2. 链表环检测(Linked List Cycle, 如LeetCode的第141题):判断链表中是否存在环,并找出环的起点。 3. 两两交换链表中的节点(Swap Nodes in Pairs, 如LeetCode的第142题):将链表中相邻的节点两两交换。 4. ...
javascript js_leetcode题解之141-linked-list-cycle.js
python python_leetcode题解之142_Linked_List_Cycle_II
Linked List Cycle**:判断链表是否有环,如有,找到环的入口节点。 - **19. Remove Nth From End**:移除链表中的第 n 个节点。 - **206. Reverse Linked List**:反转整个链表。 - **21. Merge Two Sorted ...
javascript js_leetcode题解之142-linked-list-cycle-ii.js
15. **Linked List Cycle II**:找到链表中的环的起始节点。通过修改快慢指针的速度,可以确保它们在环内相遇并能确定环的起点。 16. **Reverse Linked List**:反转链表。可以使用迭代或递归的方式,改变节点间的...
Linked List Linked List Cycle Given a linked list, determine if it has a cycle in it. public static boolean hasCycle(ListNode head) 快慢指针法,块指针从head.next开始,慢指针从head开始,快指针每次移动...
leetcode 2 sum c LeetCode 帮助文档 ...Cycle 160 Easy Intersection of Two Linked Lists 203 Easy Remove Linked List Elements no 206 Easy Reverse Linked List 234 Easy Palindrome Linked List
- **Linked List Cycle**:检测链表中的环。 - **Remove Duplicates from Sorted List**:从已排序的链表中移除重复项。 - **Merge Sorted Lists**:合并两个已排序的链表。 - **Reverse Linked List**:反转...
List LeetCode 92.Reverse Linked List II LeetCode 138.Copy List with Random Pointer LeetCode 142.Linked List Cycle II(solve1) LeetCode 142.Linked List Cycle II(solve2) LeetCode 160.Intersection of Two ...
java lru leetcode leetcode-java leetcode刷题笔记 ...141.Linked List Cycle 142.Linked List Cycle II 188.Best Time to Buy and Sell Stock IV 217.Contains Duplicate 263.Ugly Number 264.Ugly Number II
java二叉树算法源码 algorithm-primer algorithm ...List Medium 21 合并两个有序链表 Merge Two Sorted Lists Easy 141 判断链表是是否存在环 Linked List Cycle Easy 142 环形链表II Linked List Cycle I
8. 链表的环形检测(Detect Cycle in Linked List) 链表的环形检测是检测链表中是否存在环形结构。这种操作可以用于解决一些特殊的问题,例如,检测链表中的环形结构以避免死循环。 知识点:链表的环形检测的算法...
* [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) 2018.9.25 环形链表 II(Linked List Cycle II) 2018.9.26 删除排序链表中的重复元素 II(Remove Duplicates from Sorted List II) 2018.9.27 重建二叉树(Rebuild Binary Tree) ...
6. **环形链表**(Linked List Cycle II) - 知识点:链表,快慢指针法(Floyd判圈法) - 解题方法:设置两个指针,一个速度慢,每次移动一步,一个速度快,每次移动两步。如果链表存在环,它们会在环内相遇。 7....
3. **环形链表(Linked List Cycle)** - **问题描述**:给定一个链表,判断链表中是否有环。 - **解决方案**: - 使用快慢指针技术。快指针每次移动两步,慢指针每次移动一步。 - 如果链表中存在环,则快慢指针...