leetcode141

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution
    {
        public bool HasCycle(ListNode head)
        {
            //if (head == null)
            //{
            //    return false;
            //}
            //else
            //{
            //    var temp = head;
            //    while (head.next != null)
            //    {
            //        var cur = head.next;
            //        if (temp == cur)
            //        {
            //            return true;
            //        }
            //        else
            //        {
            //            head = head.next;
            //        }
            //    }
            //    return false;
            //}

            if (head == null) return false;
            ListNode walker = head;
            ListNode runner = head;
            while (runner.next != null && runner.next.next != null)
            {
                walker = walker.next;
                runner = runner.next.next;
                if (walker == runner) return true;
            }
            return false;
        }
    }

https://leetcode.com/problems/linked-list-cycle/#/description

时间: 2024-08-14 18:07:17

leetcode141的相关文章

LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

链表相关题 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? (Easy) 分析: 采用快慢指针,一个走两步,一个走一步,快得能追上慢的说明有环,走到nullptr还没有相遇说明没有环. 代码: 1 /** 2 * Definition for singly-linked list.

[LeetCode141]Linked List Cycle

题目:Given a linked list, determine if it has a cycle in it. 判断一个链表是否有环 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool hasCyc

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

LeetCode链表解题模板

一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改,比如要计算链表的长度: ListNode* p = head; int num = 0; while(p!=NULL){ num++; p = p->next; } 如果要找到最后的节点,可以更改while循环中的条件,只不过需要加上head为NULL时的判断 if(!head) return hea

算法训练营

概念:算法与数据结构相辅相成 算法是为了解决某一个具体的问题,提出来的一个解法 数据结构是为了支撑这次解法,所提出的一种存储结构 1.两数之和(LeetCode1) 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] =

链表随笔

链表数据结构: public class LinkedList{ int val; LinkedList next; public LinkedList(int val){this.val = val;} } 1\翻转链表  Leetcode 206 关键为对下一个next的备份,对上一个节点的记录(temp) /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next