Question: A linked list contains a cycle if, starting from some node p, following a sufficient number of next links brings us back to node p.

A linked list contains a cycle if, starting from some node p, following a sufficient number of next links brings us back to node p. p does not have to be the first node in the list. Assume that you are given a linked list that contains N nodes. However, the value of N is unknown.
a. Design an O(N) algorithm to determine if the list contains a cycle. You may use O(N) extra space.
b. Repeat part (a), but use only O(1) extra space.

Step by Step Solution

3.29 Rating (164 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

b Use two iterators p and q both initially at the ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Document Format (1 attachment)

Word file Icon

1486-C-S-A(309).docx

120 KBs Word File

Students Have Also Explored These Related Algorithms Questions!