算法题LC10: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 {
    public boolean hasCycle(ListNode head) {
        if(head==null)
            return false;
        ListNode fast=head;
        ListNode slow=head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
            if(fast==slow)
                return true;
        }
        return false; 
    }
}

发布了80 篇原创文章 · 获赞 1 · 访问量 1414

猜你喜欢

转载自blog.csdn.net/alidingding/article/details/104672999