Linked List
Floyd's fast and slow pointer
- C++
- Java
ListNode* slow = head;
ListNode* fast = head;
while (fast != nullptr && fast->next != nullptr) {
// do something here
slow = slow->next;
fast = fast->next->next;
}
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
// do something here
slow = slow.next;
fast = fast.next.next;
}