publicbooleanhasCycle(ListNode head){// When only 1 node of the listif(head.next ==null){returnfalse;}// Init. 2 pointersListNode slow = head, fast = head;// Iterate throuhg the listnodewhile(slow !=null|| fast.next !=null){
slow = slow.next;
fast = fast.next.next;if(slow == fast){returntrue;}}returnfalse;}
2023/12/16
一下就寫出來,但是while的條件一直出錯
publicclassSolution{publicbooleanhasCycle(ListNode head){ListNode slow = head, fast = head;while(fast !=null&& fast.next !=null){
slow = slow.next;
fast = fast.next.next;if(slow == fast)returntrue;}returnfalse;}}