Question

You have what you hope is a linked list, but you are concerned that it may...

You have what you hope is a linked list, but you are concerned that it may contain a cycle. Describe an algorithm FINDLOOP that takes a reference to the front of the list as input and either returns false if the list has no loops, true otherwise.

I would prefer the answer in pseudocode format. Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def FINDLOOP (head):
    if head=NULL   
       return false
slowPtr=head   
fastPtr=slowPtr.next   
while fastPtr!=NULL:   
   if fastPtr.next=NULL
       return false
fastPtr=fastPtr.next.next
slowPtr=slowPtr.next

if fastPtr=slowPtr
       return true
endwhile
return false   
enddef

Add a comment
Know the answer?
Add Answer to:
You have what you hope is a linked list, but you are concerned that it may...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a...

    Please write the code in a text editor and explain thank you! 1. LINKED-LIST: Implement a function that finds if a given value is present in a linked-list. The function should look for the first occurrence of the value and return a true if the value is present or false if it is not present in the list. Your code should work on a linked-list of any size including an empty list. Your solution must be RECURSIVE. Non-recursive solutions will...

  • Write a C# function bool HasCycle(Node head) that detects a cycle in a singly linked list....

    Write a C# function bool HasCycle(Node head) that detects a cycle in a singly linked list. It must return a boolean true if the list contains a cycle, or false otherwise. HasCycle has a reference to a Node object, that points to the head of a linked list, as parameter.

  • True/False Question 1. A Linked List will have no limited size. You may add or insert...

    True/False Question 1. A Linked List will have no limited size. You may add or insert as many elements as needed. True False 2. Linked List search is a linear search. Therefore it will be slow. True False 3. A JPanel can contain zero or more JPanels. True False 4. Users can interact with JLabel component. True False 5. This code is to add a panel to the south region of a JFrame JPanel panel = new JPanel (); frame.add...

  • Assume you have a linked list of integer data pointed by head (p-head). head e2 next...

    Assume you have a linked list of integer data pointed by head (p-head). head e2 next nextー next next Qu: Write the recursive method that duplicates (make a separate copy) the linked list (2 Points) Q2: Write the method that sort the linked list in increasing order (from smallest to the largest) (2 Points) Q3: Write the method that returns true if all the elements of the linked list are different. It return false otherwise (1 Point)

  • Write a Python function to implement the quick sort algorithm over a singly linked list. The...

    Write a Python function to implement the quick sort algorithm over a singly linked list. The input of your function should be a reference pointing to the first node of a linked list, and the output of your function should also be a reference to the first node of a linked list, in which the data have been sorted into the ascending order. (You may use the LinkedQueue class we introduced in the lecture directly in your program.)

  • When you are asked to develop an algorithm, you can provide either pseudocode or actual C++...

    When you are asked to develop an algorithm, you can provide either pseudocode or actual C++ code. D 1. Assume you have a singly-linked list named L. The list header contains only one pointer, to the front of the list. Write an algorithm that displays each node starting with the first. Enter answer... 2. In words, describe an approach to displaying the list L in reverse order. Remember that L's header contains only a pointer to the first node. Enter...

  • can you guys please help me with these questions thank you 9.Given a Double Linked-List that...

    can you guys please help me with these questions thank you 9.Given a Double Linked-List that contains the values in order: 1,2,3,4,5; and the following class declarations: class List private: Node* head; public: PrintBackwards (); class Node{ friend class List; private: int value; Node* next; Node* prev; Write the algorithm PrintBackwards that prints the whole list backwards. So your functionshould output: “ 5,4,3,2,1" Extra Credit if you instead implement the algorithm for a single linked-list (i.e. only using the Node*...

  • C++ You're given the pointer to the head nodes of two linked lists. Compare the data...

    C++ You're given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty. Input Format You have to complete the int CompareLists (Node headA, Node* head B) method which takes...

  • In JAVA Recursive Methods For exercises 16 to 18 assume we have a sorted linked list...

    In JAVA Recursive Methods For exercises 16 to 18 assume we have a sorted linked list of Integer. The start of the linked list is referenced by values which, of course, is of type LLNode. For example purposes, assume values points to a list containing 3, 6, 6, 9, 12, 15, 18, 19, 19, and 20. You can and should assume the list in question is sorted in nondecreasing order. For each of these exercises you should also create a...

  • I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement...

    I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement the following operations for an ordered list of integers ordered in ascending order using a doubly linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are. You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or sentinel nodes. • OrderedList ()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT