In the last part , we saw the academic (not general purpose) version of a Linked List used to solve the puzzles, and solved the following puzzles on linked list. Reverse the list recursively Reverse the list iteratively Find if a list is cyclic In this part, I will be solving the remaining two puzzles that I listed in the last part. Finding the cyclic node in a cyclic linked list According to my solution, the node which is actually supposed to be the end of the linked list is the cyclic node. Let us call Cn. Taking node Cn as the cyclic one has an advantage wherein you can break the cycle; assign Cn->next = nullptr; But some people take the node after Cn as the cyclic node. The node after Cn is the node somewhere back in the list. This way it is not possible to break the cycle as we would traversed past Cn. LinkedList::Node* LinkedList::FindCyclicNode() const { int iterCount = 0; auto jmpBy1Ptr = root; auto jmpBy2Ptr = root; while (jmpBy1Ptr !...
Another effective [debugging] technique is to explain your code to someone else. This will often cause you to explain the bug to yourself. Sometimes it takes no more than a few sentences, followed by an embarrassed "Never mind. I see what's wrong. Sorry to bother you." This works remarkbly well; you can even use non-programmers as listeners. - From "The Practice of Programming" by Brian W Kernighan & Rob Pike.