Middle of a Linked List
Write the code, see it run, pass the checks.
The problem
- easy
- Code
Write middleNode(head). head is the first node of a linked list made of nodes { value, next }, where next is the following node, or null at the end. Return the middle node itself, not its value or a copy. When the list has an even number of nodes there are two middles: return the second. Return null for an empty list, where head is null, and don't change the list. For example, the middle node of list([1, 2, 3, 4, 5]) holds 3, and that of list([1, 2, 3, 4, 5, 6]) holds 4, where list builds a linked list from an array.
Your code must pass
- Finds the middle of an odd-length list
- Returns the second middle of an even-length list
- Returns the node itself
- Handles an empty list and a single node