Skip to main content

Posts

Showing posts from November, 2012

Linked List Quiz - Part II !!!

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 recursivelyReverse the list iterativelyFind 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 != nullptr   && jmpBy2P…