CC150 2.8&&Linked List Cycle dection 判断循环LinkedList


《进军硅谷》类似题目:环的长度
题目:给出一个单向链表头指针,如果有环,判断环的长度,否则返回0;



思路:
1 判断链表是否有环。 使用快慢指针,如果快指针已经到尾部,而且他们没有相遇,则无环;如果他们相遇,则有环
2 上图为例:
慢指针:9->8->7->1;
快指针:7->2->4->1;
判断是否有环:在快指针到尾前,快慢指针能否相等;如果相等,则有环;否则无环

计算环的长度:快慢指针相等时,从慢指针开始,遍历一遍环,计算环的长度;


C#代码如下:
判断是否有环版本:
<span style="white-space:pre">	</span>/// <summary>
        /// 判断LinkedList中是否有闭环。
        /// </summary>
        /// <param name="head"></param>
        /// <returns></returns>
        public static bool HasCircle(LinkedListNode<int> head)
        {
            LinkedListNode<int> slow = head;
            if (slow == null || slow.Next == null)
                return false;
            LinkedListNode<int> fast = slow.Next.Next;

            //使用快慢指针,如果快指针走到尾前,两根指针遇到,就有闭环
            while (fast != null && fast.Next != null)
            {
                if (slow == fast) return true;
                slow = slow.Next;
                fast = fast.Next.Next;
            }
            return false;
        }


C#计算环的长度:
<span style="white-space:pre">	</span>/// <summary>
        /// 判断LinkedList中是否有闭环。
        /// </summary>
        /// <param name="head"></param>
        /// <returns></returns>
        public static int GetCircleLength(LinkedListNode<int> head)
        {
            LinkedListNode<int> slow = head;
            if (slow == null || slow.Next == null)
                return 0;
            LinkedListNode<int> fast = slow.Next.Next;

            //使用快慢指针,如果快指针走到尾前,两根指针遇到,就有闭环
            while (fast != null && fast.Next != null)
            {
                if (slow == fast) return GetLength(slow);
                slow = slow.Next;
                fast = fast.Next.Next;
            }
            return 0;
        }


        private static int GetLength(LinkedListNode<int> node)
        {
            int length = 1;
            LinkedListNode<int> curr = node;
            while (curr.Next != node)
            {
                //转一圈,并计算长度
                length++;
                curr = curr.Next;
            }
            return length;
        }


Leetcode原版题目: https://leetcode.com/problems/linked-list-cycle/
答案:
/**
 * 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) {
        ListNode slow = head;
            if (slow == null || slow.next == null)
                return false;
            ListNode fast = slow.next.next;

            //使用快慢指针,如果快指针走到尾前,两根指针遇到,就有闭环
            while (fast != null && fast.next != null)
            {
                if (slow == fast) return true;
                slow = slow.next;
                fast = fast.next.next;
            }
            return false;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值