The solution seems to be missing the logic of “Checks all nodes are identical” although stated in the comment. Shouldn’t it have if(start->data == start->next->data) {return random_pointer->data} before locating the middle node? Thanks.
Question about 8.16 in 1.4.7
Hey Ray,
I think the comment is not super clear is the main problem. In the do while loop, our ending condition is while (curr != random_pointer) which basically limit us traverse each node at most once therefore if we reach curr again means all nodes are identical. Like you said, there is no explicit check for that so we probably can remove this comment.
Hi Tsung-Hsien,
Thanks for your reply. What I mean is when all nodes are identical, the solution does not return immediately but still traverses half of the list to find the middle node.
Yes, you definitely can do that small optimization (a bool variable with few lines of code), which shall happen very rarely. Compared with not setting correct ending condition which is for all identical nodes, I think that is not worth it because the time complexity is still O(n).