The problem. Given the head of a singly linked list, return the middle node. If there are two middle nodes (an even number of nodes), return the second one.
Input: head = [1, 2, 3, 4, 5]
Output: [3, 4, 5] the middle node is 3
Input: head = [1, 2, 3, 4, 5, 6]
Output: [4, 5, 6] 3 and 4 are both middles; return the secondWith an array you'd just read arr[n / 2]. A linked list has no indexes and doesn't know its own length — that's what makes this interesting.
Free account
Sign up to read the rest of this lesson: 5 more sections, 2 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come