We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Correct, but that's a red herring. My first attempt did a Node==Node check and this is what I returned to. I tried Node.data==Node.data as I was troubleshooting, but this isn't a good way to test (since it fails if the linked list has duplicate data values). The problem with my implementation was where I started the two node references. I initialized both at head, did my checks, then incremented at different speeds. Obviously if they start at the same position and I check to see if the nodes are the same, they're going to fail. The way I fixed it was by initializing one node at head and the other at head.next. Solved.
Cycle Detection
You are viewing a single comment's thread. Return to all comments →
Correct, but that's a red herring. My first attempt did a Node==Node check and this is what I returned to. I tried Node.data==Node.data as I was troubleshooting, but this isn't a good way to test (since it fails if the linked list has duplicate data values). The problem with my implementation was where I started the two node references. I initialized both at head, did my checks, then incremented at different speeds. Obviously if they start at the same position and I check to see if the nodes are the same, they're going to fail. The way I fixed it was by initializing one node at head and the other at head.next. Solved.