Implement the following two methods in O(n) time.   // Reverse the list and return it in O(n) time   public static ArrayList reverse(ArrayList list)     // Reverse the list and return it in O(n) time   public static LinkedList reverse(LinkedList list)   Use the following code to test these methods:   public static void main(String[] args) {     Scanner input = new Scanner(System.in);     System.out.print("Enter 10 numbers: ");     ArrayList list = new ArrayList<>();     for (int i = 0; i < 10; i++) {       list.add(input.nextInt());     }     reverse(list);     for (int i: list) {       System.out.print(i + " ");     }     System.out.println();         LinkedList list1 = new LinkedList<>(list);     reverse(list1);     for (int i: list1) {       System.out.print(i + " ");     }     System.out.println();      }

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question

Implement the following two methods in O(n) time.

  // Reverse the list and return it in O(n) time

  public static <E> ArrayList<E> reverse(ArrayList<E> list)

 

  // Reverse the list and return it in O(n) time

  public static <E> LinkedList<E> reverse(LinkedList<E> list)

 

Use the following code to test these methods:

  public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.print("Enter 10 numbers: ");

    ArrayList<Integer> list = new ArrayList<>();

    for (int i = 0; i < 10; i++) {

      list.add(input.nextInt());

    }

    reverse(list);

    for (int i: list) {

      System.out.print(i + " ");

    }

    System.out.println();

   

    LinkedList<Integer> list1 = new LinkedList<>(list);

    reverse(list1);

    for (int i: list1) {

      System.out.print(i + " ");

    }

    System.out.println();   

  }

 

Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Threads in linked list
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning