https://leetcode.com/problems/middle-of-the-linked-list/
https://leetcode.com/problems/middle-of-the-linked-list/discuss/154619/C%2B%2BJavaPython-Slow-and-Fast-Pointers
Given a non-empty, singly linked list with head node
head
, return a middle node of linked list.
If there are two middle nodes, return the second middle node.
public ListNode middleNode(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}