The problem asks for the middle node to be returned. The example returns a list starting at the middle node. Not quite the same thing.
On a related note, since lists conform to the Collection protocol which has a count property, wouldn’t it be simpler to return list.node(at: list.count / 2)
? (I know, it avoids using the list structure being taught )
I know the node/list duality is a little ambiguous, I’m open to suggestions
An idea I’ve had is to instantiate a brand new node with the same value as the middle node, but then I would argue that isn’t the real “middle node” of the list.
Note that list.count
is O(n)
and node(at:)
is also O(n)
.
Thanks Kevin, I had forgotten that count is O(n).