141. Linked List Cycle(判断l链表是否有环)(leetcode)

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.

Follow up:

Can you solve it using O(1) (i.e. constant) memory?

方法一:双指针

1、一个指针移动一步,一个指针移动两步,如果两个指针相遇则有环。

2、做任何题,第一个应该想的是鲁棒性

原文地址:https://www.cnblogs.com/shaer/p/10419736.html

时间: 2024-10-04 03:11:53

141. Linked List Cycle(判断l链表是否有环)(leetcode)的相关文章

LeetCode 141. Linked List Cycle(判断链表是否有环)

题意:判断链表是否有环. 分析:快慢指针. /** * 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

[LeetCode] Linked List Cycle II 单链表中的环之二

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 这个求单链表中的环的起始点是之前那个判断单链表中是否有环的延伸,可参见我之前的一篇文章 (http://www.cnblogs.com/grandyang/p/4137187.html). 还是要设

[leetcode]141. 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? 题意: 给定一个链表,判断是否有环 思路: 快慢指针 若有环,则快慢指针一定会在某个节点相遇(此处省略证明) 代码: 1 public class Solution { 2 public boolean hasCycle(ListNode head) { 3 ListNode f

141 Linked List Cycle(判断链表是否有环Medium)

题目意思:链表有环,返回true,否则返回false 思路:两个指针,一快一慢,能相遇则有环,为空了没环 ps:很多链表的题目:都可以采用这种思路 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 1

141. Linked List Cycle 判断链表是否有环

class Solution { public: bool hasCycle(ListNode *head) { if(head == NULL) return false; if(head->next == NULL) return false; ListNode* p1 = head; ListNode* p2 = head; p1 = p1->next; p2 = p2->next->next; while(p2 != NULL && p2->next

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? 注意,链表循环并不是尾指针和头指针相同,可能是在中间某一段形成一个环路,所以不能只判断元素和第一个元素是否存在重合 先设置两个指针p_fast和p_slow.从头开始遍历链表,p_fast每次走两个节点,而p_slow每次走一个节点,若存在循环,这两个指针必定重合: 1 /**

leetCode 141. Linked List Cycle 链表

141. 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? 题目大意: 判断一个单链表是否存在环. 思路: 采用快慢指针来处理. 代码如下: /**  * Definition for singly-linked list.  * struct ListNode {  *     int va

<LeetCode OJ> 141. Linked List Cycle

141. Linked List Cycle My Submissions Question Total Accepted: 88665 Total Submissions: 241622 Difficulty: Medium Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? Subscribe to see which co

[LeetCode]93. Linked List Cycle II查找链表中环的起始节点

Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note: Do not modify the linked list. Follow up:Can you solve it without using extra space? Subscribe to see which companies asked this question 解法:这道题是题目L