// Initialize slow & fast pointers
ListNode slow = head;
ListNode fast = head;
/**
* Change this condition to fit specific problem.
* Attention: remember to avoid null-pointer error
**/
while (slow != null && fast != null && fast.next != null) {
slow = slow.next; // move slow pointer one step each time
fast = fast.next.next; // move fast pointer two steps each time
if (slow == fast) { // change this condition to fit specific problem
return true;
}
}
return false; // change return value to fit specific problem
// Initialize slow & fast pointers
var slow *ListNode = head
var fast *ListNode = head
/**
* Change this condition to fit specific problem.
* Attention: remember to avoid null-pointer error
**/
for slow != nil && fast != nil && fast.Next != nil {
slow = slow.Next // move slow pointer one step each time
fast = fast.Next.Next // move fast pointer two steps each time
if slow == fast { // change this condition to fit specific problem
return true
}
}
return false // change return value to fit specific problem
[Go] Linked List - Remove Linked List Elements (0) | 2022.05.05 |
---|---|
[Go] Linked List - Reverse Linked List (0) | 2022.05.02 |
[Go] Linked List - Remove Nth Node From End of List (0) | 2022.05.01 |
[Go] Linked List - Linked List Cycle2(Detect Cycle) (0) | 2022.05.01 |
[Go] Linked List - Linked List Cycle (0) | 2022.04.30 |
댓글 영역