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?

此题并不难,声明2个指针一个步长1,一个步长2,互相追逐,如果追上了就说明有循环,就是没有判断链表是否为空卡在那里很多时间。。。不说了。。。

附上c代码:

/* Definition for singly-linked list.
 struct ListNode {
     int val;
     struct ListNode *next;
 };
*/ 

bool hasCycle(struct ListNode *head) {
    if(head!=NULL)
    {
        struct ListNode *p=head->next;
        struct ListNode *q=p;
        while(p!=NULL&&q!=NULL&&q->next!=NULL)
        {
            p=p->next;
            q=q->next->next;
            if(p==q)
                return true;
        }
    }
    return false;
}

  

时间: 2024-10-11 10:35:06

leetcode-141的相关文章

LeetCode 141, 142. Linked List Cycle I+II

判断链表有没有环,用Floyd Cycle Detection算法,用两个快慢指针. class Solution { public: bool hasCycle(ListNode *head) { if (!head) return false; ListNode *slow, *fast; slow=fast=head; do{ if (fast==NULL || fast->next==NULL) return false; slow = slow->next; fast = fast-

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 141 Linked List Cycle Hash fast and slow pointer

Problem describe:https://leetcode.com/problems/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 lis

LeetCode(141): 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? 题意:判断一个链表中是否存在环. 思路:(参考别人的做法)采用"快慢指针"检查链表是否含有环.即让一个指针一次走一步,另一个指针一次走两步,如果链表中含有环,快的指针会再次和慢的指针相遇. 代码: public boolean h

(LeetCode 141/142)Linked List Cycle

1.Given a linked list, determine if it has a cycle in it. 2.Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 3.Given a linked list, return the length of the cycle, if there is no cycle, return 0; 题目要求: 1

leetcode 141 142. 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) { if(head ==

Leetcode 141. Linked List CycleJAVA语言

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.  * class ListNode {  *     int val;  *     ListNode next;  *     ListNode(int x) {  *

leetcode || 141、Linked List Cycle

problem: Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? Hide Tags Linked List Two Pointers thinking: (1)如果可以开设额外的空间,使用unordered_set存储遍历过的结点,出现重复时即为存在环形结构 (2)如果不适用额外的空间,及空间复杂度为O(1),这里使用快.

[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

LeetCode 141. 环形链表

给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 示例 1: 输入:head = [3,2,0,-4], pos = 1输出:true解释:链表中有一个环,其尾部连接到第二个节点. 示例 2: 输入:head = [1,2], pos = 0 输出:true 解释:链表中有一个环,其尾部连接到第一个节点. 示例 3: 输入:head = [1], pos = -1