AlgoMaster Logo

Middle of the Linked List

values=[1, 2, 3, 4, 5]
1public ListNode middleNode(ListNode head) {
2    ListNode slow = head;
3    ListNode fast = head;
4
5    while (fast != null && fast.next != null) {
6        slow = slow.next;
7        fast = fast.next.next;
8    }
9
10    return slow;
11}
0 / 7
12345head