Practice

Access

Using the methods outlined in the “Benefits of LinkedLists” section, fill both a LinkedList and an ArrayList with 10,000 elements. Then, find the time it takes to access the last element of each list. Determine which type of list can access elements more efficiently. Hint: You can use the get(int position) method to access the element at a given position of a list.

💻 Template code

Solution code

Names

Create a LinkedList of Strings, and ask the user to input 10 names one by one into the list. Names.java

  1. Output this list.
  2. Remove each name that has 5 or fewer letters, and output the new list.
  3. Add the word “Apple” to the beginning of the list.
  4. Add the word “Peanut” to the second-to-last position of the list. (Hint: Make sure to take advantage of LinkedList methods!)
  5. Output the new list.

💻 Template code

Solution code

Sort

Write a method which takes in a LinkedList of Strings and rearranges the elements (without creating a new list) so it is sorted in ascending order of String length. Don’t use any pre-existing sort methods… write your own. :)

💻 Template code

Solution code

Linkedlist - DIY

Implement a simple LinkedList

You will need to create a LinkedListNode class, which represents each item/node in the linked list: