Question

6. Suppose a Java method receives a List<Integer> and reverses the order of the items it contains by removing each item from

Please explain clearly. Thank you for your time and help will upvote!

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Operations needs to do when you want to reverse list are
1. Removing item from front of the list
2. Pushing it to stack
3. popping items from stack
4. Inserting each item to the end of the list

Stack operations are done at time complexity of O(1)

List operations from above are
1. Removing item from front of the list
2. Inserting each item to the end of the list

=====================================

a) When ArrayList is passed
1. Removing item from front of the list
Time complexity to do this is O(n)
Because when we want to remove front element then we need to shift all the items in the list to left
 
2. Inserting each item to the end of the list
Time complexity = O(1)
Because we just need to put an item at end and increment size by 1

So, Total time complexity for 4 operations are = O(n) + O(1) + O(1) + + O(1) = O(n)

=====================================

b) When LinkedList is passed
1. Removing item from front of the list
Time complexity to do this is O(1)
Because when just need to move front to its next 
 
2. Inserting each item to the end of the list
Time complexity = O(n)
Because we need traverse from front to end and then we remove the link of the last item

So, Total time complexity for 4 operations are = O(1) + O(n) + O(1) + + O(1) = O(n)
Add a comment
Know the answer?
Add Answer to:
Please explain clearly. Thank you for your time and help will upvote! 6. Suppose a Java...
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 that uses a stack to reverse its inputs. Your stack must be generic...

    Write a program that uses a stack to reverse its inputs. Your stack must be generic and you must demonstrate that it accepts both String and Integer types. Your stack must implement the following methods: push, pop, isEmpty (returns true if the stack is empty and false otherwise), and size (returns an integer value for the number of items in the stack). You may use either an ArrayList or a LinkedList to implement your stack. Also, your pop method must...

  • JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class....

    JAVA PROGRAMMING PLEASE This lab has three parts: Create an ArrayList class. Create a LinkedList class. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them. Task 1 – ArrayList Class Create an ArrayList class. This is a class that uses an internal array, but manipulates the array so that the array can be dynamically changed. This class should contain a default and overloaded constructor, where the default...

  • Please help JAVA Create the following in Stack.java: An array of ints set to size 10...

    Please help JAVA Create the following in Stack.java: An array of ints set to size 10 to represent the Stack, an int variable for the index A constructor that sets the index to -1 A method called isEmpty (returns a boolean) that has no parameters. This method returns true if the Stack is empty and false if the Stack is not empty. true A method called push(returns a Boolean) that takes an int as a parameter. This method pushes, or...

  • Please answer the question in the picture below and explain your answer. If it helps, the...

    Please answer the question in the picture below and explain your answer. If it helps, the associated coursework is taught in Java. 12. Given the following method, Write the output when the following method is passed each of the following lists: public static void mystery (ArrayList<Integer> list) { for (int i = 0; i < list.size()-1; i++) { int e = list.get(i); list.add (i+2, e + 3); list.remove(i); System.out.println(list); a. [80,60,50,40,30] b. (5,5,5,5,3]

  • Please help with Java array list: import java.util.ArrayList; public class ListUpdate { /** * Does a...

    Please help with Java array list: import java.util.ArrayList; public class ListUpdate { /** * Does a linear search through the ArrayList list, returning the index of the first occurrence of * valToFind. * * Starting from index 0, check each element in list and return the index of the first element * that matches valToFind. * * @param valToFind The int value to search for. * @param list The ArrayList of Integers to search in. * @return The index of...

  • In JAVA, please. THANK YOU In this project you will create a generic linked list using...

    In JAVA, please. THANK YOU In this project you will create a generic linked list using Java Generics. Description: Create a generic class called GenLinkedList. GenLinkedList will use nodes that store a value of the generic type to store its contents. It should have the following methods. The methods should all operate on the object making the call (none are static). Perform checking of the parameters and throw exceptions where appropriate. The linked list should be singly-linked. It should not...

  • THIS IS IN THE JAVA SCRIPT CODE ALSO PLEASE SEND ME THE CODE TYPED THANK YOU....

    THIS IS IN THE JAVA SCRIPT CODE ALSO PLEASE SEND ME THE CODE TYPED THANK YOU. Program 1 Write an inheritance hierarchy of three-dimensional shapes. Make a top-level shape interface that has methods for getting information such as the volume and the surface area of a three-dimensional shape. Then make classes and subclasses that implement various shapes such as cube, cylinders and spheres. Place common behavior in superclasses whenever possible, and use abstract classes as appropriate. Add methods to the...

  • Need assistance with this problem. This is for a java class and using eclipse. For this...

    Need assistance with this problem. This is for a java class and using eclipse. For this assignment we’ll be doing problems 21.3 and 21.4 from your textbook. Problem 21.3 asks you to write a generic method that removes duplicate items from an ArrayList. This should be fairly straightforward. Problem 21.4 asks you to implement insertion sort within a generic method. Insertion sort is described in detail on pages 250-252 of your textbook. You can write your two methods in a...

  • n JAVA, students will create a linked list structure that will be used to support a...

    n JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...

  • please answer the question in java. Thank you! 6:02 PM Wed Jan 29 83% stepik.org stepik...

    please answer the question in java. Thank you! 6:02 PM Wed Jan 29 83% stepik.org stepik 2222222222222222 NH Nathanael UCSD CSE 11 Course progress: 63/92 We have declared a method called addSorted with one ArrayList<Integer> parameter sortedNums and one int parameter newNum. You can assume sortedNums is on-empty and is already sorted in ascending (i.e., increasing) order. TASK: Fill in the body of the addSorted method such that it will add newNum to sortedNums while maintaining sorted order. 1 Week...

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