文件名称:leetcode怎么销号-LeetCode-Top-Interview-Questions:LeetCode-Top-Interview-Qu
文件大小:114KB
文件格式:ZIP
更新时间:2024-07-19 22:25:03
系统开源
leetcode怎么销号 LeetCode便签 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? 解决思路 声明一个慢指针和一个快指针 慢指针一次后移一个节点,快指针一次后移两个节点 不断后移至指针指空,或两指针相遇 代码 class Solution { public boolean hasCycle(ListNode head) { 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 fa