[Linked List]Linked List Cycle,Linked List Cycle II

一.Linked List Cycle

Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space?

(M) 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:
    bool hasCycle(ListNode *head) {
        if(head==NULL || head->next==NULL){
            return false;
        }
        ListNode *first =head;
        ListNode *second = head;
        while(first && second)
        {
            first = first->next;
            second = second->next;
            if(second){
                second = second->next;
            }
            if(first==second){
                return true;
            }
        }
        return false;
    }
};

Next challenges: (M) Linked List Cycle II

二.Linked List Cycle II

Total Accepted: 61662 Total Submissions: 196038 Difficulty: Medium

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?

(M) Linked List Cycle (H) Find the Duplicate Number

时间: 2024-11-09 03:41:07

[Linked List]Linked List Cycle,Linked List Cycle II的相关文章

LeetCode之“链表”:Linked List Cycle && Linked List Cycle II

1. 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 bool hasCycle(ListNode *head) { 2 ListNode *a = head, *b = head; 3 while(a) 4 { 5 b =

leetcode. Linked List Cycle && Linked List Cycle ii

Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 使用双指针fast和slow,fast每次走两步,slow每次走一步,如果fast追上slow则存在环,否则不存在. 1 bool hasCycle(ListNode *head) 2 { 3 ListNode *slow = head, *fast = head; 4 whil

Linked List Cycle && 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 查看是否有环,快慢两

LeetCode解题报告:Linked List Cycle && Linked List Cycle II

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 Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follo

Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up: Can

Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follow up: 思路: 1)同linked-list-cycle-i一题,使用快慢指针方法,判定是否存在环,并记录两指针相遇位置(Z): 2)将两指针分别放在链表头(X)和相遇位置(Z),并改为相同速度推进,则两指针在环开始位置相遇(Y). 证明如下: 如下图所示,X,Y,Z分别为链表起始位置,环开始位

LeetCode Linked List Cycle & Linked List Cycle II题解

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? 题目的意思就是在利用O(1)的空间判断一个链表是否存在环.我一开始的想法就是,每访问 一个节点,就遍历这个节点前面的所有节点,然后判断是否相同,如果相同就说明有环.如果所有的节点都访问完了还是没有发现有节点相同,则说明该链表没有环.但是这个想法很

[Linked List]Intersection of Two Linked Lists

Total Accepted: 53721 Total Submissions: 180705 Difficulty: Easy Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 c1 → c2 → c3 B: b1 → b2 → b3 begin

Reversed Linked List(Reverse a singly linked list)

struct ListNode { int m_nKey; ListNode* next; } ListNode* reverseList(ListNode* pHead) { ListNode* pReversedHead = nullptr; ListNode* pNode = pHead; ListNode* pPrev = nullptr; while(pNode != nullptr){ ListNode* pNext = pNode->next; if(pNext == nullpt

[Cycle.js] The Cycle.js principle: separating logic from effects

The guiding principle in Cycle.js is we want to separate logic from effects. This first part here was logical, and this second part here was effects. Effects are everything that change the external world somehow, the real world such as the DOM is con