Back to course home
0% completed
Vote For New Content
Start of LinkedList Cycle - A concise LeetCode solution!
Adrian Adewunmi
May 16, 2024
Good job on this course, Design Guru. However, LeetCode has published a more concise solution (in java).
// Design Guru Problem Statement: Start of LinkedList Cycle // LeetCode Question: 142. Linked List Cycle II public class Problem_3_Start_Of_LinkedList_Cycle { class ListNode{ int val = 0; ListNode next; public ListNode(int value){ this.val = value; } } public ListNode findCycleStart(ListNode head){ ListNode slow = head; ListNode fast = head; while(fast != null && fast.next != null){ slow = slow.next; fast = fast.next.next; if (slow == head) { break; } } if (fast == null || fast.next == null) { return null; } fast = head; while(slow != fast){ fast = fast.next.next; slow = slow.next; } return slow; } }
2
0
Comments
Comments
Ezequiel Gimeneza year ago
This solution contains errors
if (slow == head) { break; }
should be
if (slow == fast) { break; }