Question

You are given a pointer head to the first node in a linked list. Each node...

You are given a pointer head to the first node in a linked list. Each node points to the next node. We call the list a snail if the last node points to some node v in the list. We call the list a snake if the last node points to NULL. The list has n nodes. You are not allowed to change the list (permanently or temperately). You are allowed to use O(1) extra memory. The value of n is not known to you.

Suggest an algorithm that the pointer to the first node, and after O(n) time and identify if the list is a snake or a snail.

please be detailed in response thanks!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  1. START
  2. READ pointer HEAD
  3. set ptr= HEAD
  4. Repeat steps 5- 9 until ptr->next!=NULL OR ptr->NEXT!= HEAD
  5. if ptr->next== NULL execute steps 6
  6. Set type=snake
  7. if ptr->next== HEAD
  8. Set type=snail
  9. ptr=ptr->next
  10. DISPLAY type
  11. STOP

The control keep on traversing until it encounters a NULL or HEAD values. If it encounters a NULL value then its a snake. If it encounters a Head value then its snail type.

Add a comment
Know the answer?
Add Answer to:
You are given a pointer head to the first node in a linked list. Each node...
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
  • c++ 5. You're given the pointer to the head node of a sorted linked list, where...

    c++ 5. You're given the pointer to the head node of a sorted linked list, where the data in the nodes is in ascending order. Delete as few nodes as possible so that the list does not contain any value more than once. The given head pointer may be null indicating that the list is empty. Enter numbers ending with -999 for the linked list: 3 7 11 11 11 24 27 30 42 42 47 55 55 78 89-999...

  • Given the node structure and the head pointer (headptr) of a linked list write a code...

    Given the node structure and the head pointer (headptr) of a linked list write a code to move the last element to the head of the list. struct node {    int value;    struct node *next; };

  • Answer all questions 1- in circular singly linked list, previous pointer of the first node points...

    Answer all questions 1- in circular singly linked list, previous pointer of the first node points to which node A. First node B. Itself C. null D. Last node 2- Which of the following is NOT an applications of linked lists? A. Implementation of stacks and queues B. Dynamic memory allocation C. Manipulation of polynomials D. Keeping people at home during epidemics like corona virus 3- In a circular singly linked list? A. Components are all linked together in some...

  • Problem Statement This problem provides you with a linked list composed of node objects chained together...

    Problem Statement This problem provides you with a linked list composed of node objects chained together via node pointers. Each node has a next pointer which points to the next node in the chain. The last node is identified by having a NULL (or nullptr) next pointer. The linked lists for this problem store string data. Your task: identify the longest string stored in the linked list. If two strings are of the same length, return the string that occurred...

  • 1. A linked list does not need to have a pointer that points to the first...

    1. A linked list does not need to have a pointer that points to the first node of the list. True or False? 2. A linked list needs to have a pointer to point to the last node of the list.  True or False? 3. If "head" is the only pointer that points to the first node of a linked list, to traverse the list, you should move the head pointer to each node one at a time.  True or False? Please...

  • Given the following linked list structure called node: struct node { int val; struct node *...

    Given the following linked list structure called node: struct node { int val; struct node * ptrNext; }; Assume we have a single list created from this structure with a head pointer called ptrFirst which is declared in the global scope. a. Write a complete C function called CountEven to count all the even values in this singly linked list of arbitrary number of nodes using an iterative (non-recursive) approach. The function takes as parameter the pointer to the starting...

  • given a pointer to the first node of a linked list, you are asked to reverse...

    given a pointer to the first node of a linked list, you are asked to reverse the list. Explain, in English, not code, how you would complete such a task if you had a limited amount of memory (not enough to store the whole list twice!)

  • Extend Linked List in C // Exercise 5 /* Parameter head points to the first node in a linked list, or is * NULL if the l...

    Extend Linked List in C // Exercise 5 /* Parameter head points to the first node in a linked list, or is * NULL if the list is empty. * * Parameter other points to the first node in a linked list, or is * NULL if the list is empty. * * Extend the linked list pointed to by head so that it contains * copies of the values stored in the linked list pointed to by other. *...

  • /*Given the head of a linked list, find and return the kth node of the linked...

    /*Given the head of a linked list, find and return the kth node of the linked list *Assume head is the first node *If k is larger than the size of the linked list, return NULL * c++ * Example: n1 -> n2 -> n3 -> n4 -> n5, k = 3 * Return &n3 */ Node* findKthNode(Node *head, int k){ return NULL; //STUB: edit with the correct output, according to the lab instructions, using recursion } must use recursive...

  • ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a...

    ***CODE MUST BE IN C++*** Using the linked list in "basic linked list" which has a STRUCTURE for each node, write FUNCTION which starts at the head and outputs the value for each node until the last node is reached. Note: your function should work with the structure that is in this program. Please do not use the example which is for a class, but this example canbe helkpful.  Also note that the function must work no matter how many nodes...

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