LeetCode: Linked List Cycle 解题报告

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?

SOLUTION 1:

经典快慢指针问题。如果存在环,fast, slow必然会相遇。就像2个速度不一样的人在环形跑道赛跑,总有一个时间他们会相遇。

如果fast到达了null,就是没有环,可以返回false.

 1 package Algorithms.list;
 2
 3 import Algorithms.algorithm.others.ListNode;
 4
 5 /**
 6  * Definition for singly-linked list.
 7  * class ListNode {
 8  *     int val;
 9  *     ListNode next;
10  *     ListNode(int x) {
11  *         val = x;
12  *         next = null;
13  *     }
14  * }
15  */
16 public class HasCycle {
17     public boolean hasCycle(ListNode head) {
18         if (head == null) {
19             return false;
20         }
21
22         ListNode slow = head;
23         ListNode fast = head;
24
25         while (fast != null && fast.next != null) {
26             slow = slow.next;
27             fast = fast.next.next;
28             if (slow == fast) {
29                 return true;
30             }
31         }
32
33         return false;
34     }
35 }
时间: 2024-10-11 13:41:35

LeetCode: Linked List Cycle 解题报告的相关文章

LeetCode: Linked List Cycle解题报告

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? SOLUTION 1: 依次将链表节点添加入已知集合,添加前检查该节点是否已经存在于已知集合中.若有,则说明链表带环;若链表所有点均能加入已知集合,则说明链表无环. 加入第 i 个链表节点是查找耗时(i-1),最坏情况时有n个节点加入,即无环

leetcode --- Linked List Cycle [Floyd's cycle-finding algorithm]

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? Linked List Two Pointers ''' Created on Nov 13, 2014 @author: ScottGu<[email protected], [email protected]> ''' # Definit

Leetcode:Linked List Cycle 链表是否存在环

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? 解题分析: 大致思想就是设置两个指针,一个指针每次走两步,一个指针每次走一步,如果这两个指针碰头了,那么一定就存在环 可以类比两个人在环形操场跑步,同时出发,一个跑得快,一个跑得慢,如果跑得快的人追上跑得慢的人,那么跑得快的人相当于多跑了一整

LeetCode: Pascal&#39;s Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

LeetCode Linked List Cycle II

/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head;

LeetCode: Linked List Cycle [141]

[题目] Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? [题意] 判断一个单向链表是否有环 [思路] 维护两个指针p1和p2,p1每次向前移动一步,p2每次向前移动两步 如果p2能够追上p1,则说明链表中存在环 [代码] /** * Definition for singly-linked list. * struct L

LeetCode: Linked List Cycle II [142]

[题目] 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? [题意] 给定一个单向链表,如果链表有环,则返回环开始的位置. [思路] 仍然是维护两个指针, p1, p2, p1每次走一步, p2每次走两步 假设进入环之前要走X步,环长为y步,p2第

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

[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? 从问题来看,如果可以充分利用额外空间的话,这个题目是不难的,然而题目提出了一个要求,能否在不使用任何额外空间的情况下解决这个问题. 通过反复思考,我觉得这题类似于追击问题,可以用一个