Question

ngu Cons eY Ja Question 1 a) Write pseudo code to output a singly-linked list in reverse order when you are NOT allowed to al

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

(a) Node reverseWIthoutDynamic(Node node) {
   Node prev = null;
   Node current = node;
   Node next = null;
   while (current != null) {
   next = current.next;
   current.next = prev;
   prev = current;
   current = next;
   }
   node = prev;
   return node;
   }
   // Time complexity = O(N)
(b) Node reverseWIthDynamic(Node node) {
   Node newList = null;
   Node current = node;
  
   while (current != null) {
   Node newnode = new Node(current.data);
   newnode.next = newList;
   newList = newnode;
   }
  
   return newList;
   }
   // Time complexity = O(N)

(c)

We can find minimum element in O(Logn) using Binary Search.

The minimum element is the only element whose previous is greater than it. We check this condition for middle element by comparing it with (mid-1)’th and (mid+1)’th elements.
If minimum element is not at middle (neither mid nor mid + 1), then minimum element lies in either left half or right half.
If middle element is smaller than last element, then the minimum element lies in left half.
Else minimum element lies in right half.

Add a comment
Know the answer?
Add Answer to:
ngu Cons eY Ja Question 1 a) Write pseudo code to output a singly-linked list in...
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
  • Write a program void reverse_list(list **l)in pseudo-code or C++ to reverse the direction of a given...

    Write a program void reverse_list(list **l)in pseudo-code or C++ to reverse the direction of a given singly-linked list. In other words, after the reversal all pointers should now point backwards. Your algorithm should take linear time. The node of this singly-linked list is defined as typedef struct list { item_type item; struct list ∗ next ; } list; void reverse_list(list **l){ } Remark: 15 points for completely correct. 10 points for correctly reversing the list with minor errors.

  • 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.)

  • Write a method max() which take a generic singly linked list as an argument and returns...

    Write a method max() which take a generic singly linked list as an argument and returns the maximum element reference in the list.      If the list is empty, please throw an empty collection exception.      The method prototype is defined as follows. Please write down your code in the method body enclosed in braces: public static <T extends Comparable<T>> T max(SingleLinkedList<T> list) throws EmptyCollectionException { } 2) What is the big O notation of the max method? a) O(N)...

  • Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying...

    Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying structure. Include the following methods: 1. insert an element within the linked list.(this should also work for the front and the rear of the list) 2. Remove an element from the linked list 3. Display (print) the elements of the linked list in order. 4. A method to check if the list is "empty". Test your solution using a linked list that initially has...

  • Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list...

    Q.(1)Describe the algorithm and java implementation for the following operations A. Create a singly linked list L1 with 4 nodes. You can use insert operation to add nodes to the list. Each element represent an airport code (e.g. BOS, ATL, JFK, MSP, etc.). Display the list L1 after it is created. B. Given singly linked list L1, create another singly linked list L2 that contains the same elements but in the reverse order. Display the content of both L1 and...

  • Submit the pseudo code. Write a member function PrintReverse that prints the elements on a list...

    Submit the pseudo code. Write a member function PrintReverse that prints the elements on a list in reverse order. For instance, for the list X Y Z, list. PrintReverse() would output element in the list. You may assume that the list is not empty.

  • Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using...

    Arrays, Lists, Stacks and Queues: 1) Write C++ code to reverse a singly-linked list L using only a constant amount of additional storage and no recursion. Assume the list object has a head pointer _head and consists of Node objects; a Node object has a pointer Node* _next

  • C++ Write a function that takes in as input a singly linked list that contains the...

    C++ Write a function that takes in as input a singly linked list that contains the digits to an integer in reverse order. For example: 0 → 0 → 1 = 100 or 3 → 2 → 1 = 123. Return the list as the number. You may assume there will be no leading zeros in the list. For example: 3 → 2 → 0. You may assume there will not be any negative integers.

  • IMPLEMENT AN IMMUTABLE SINGLY LINKED LIST IN JAVASCRIPT ------------------------------------------------------------------------------------------------------------------------- So I am trying to implement a...

    IMPLEMENT AN IMMUTABLE SINGLY LINKED LIST IN JAVASCRIPT ------------------------------------------------------------------------------------------------------------------------- So I am trying to implement a singly linked list that given the below function will pass the bottom four tests when ran. Please help me generate the singly linked list that will work and pass the tests. I will only use this as a reference so please add comments to explain what you are doing so I can understand and write this myself. I think a join method may need...

  • In Java: Assume the "ferrets" variable points to a linked list of "LLNode." Write code that...

    In Java: Assume the "ferrets" variable points to a linked list of "LLNode." Write code that traverses the list and prints the following. Do not forget to consider the case where the list is empty. The "LLNode" class is given: public class LLNode { protected T info; protected LLNode link; public LLNode(T info) { this.info = info; link = null; } public void setInfo(T info) { this.info = info; } public T getInfo() { return info; } public void setLink(LLNode...

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