Please do it in simple Java code. no C++ or anything. Problem 1 a Java program that:
Question:
Please do it in simple Java code. no C++ or anything.
Problem 1
a Java program that:
- a link list having these values in linked list: 4 -> 7 -> 8-> 1 -> 2;
- Prints out the linked list by writing a method that iterates through the linked list.
- A method that given the head of the list, returns the mth to the last element. For example, given the above list and 4 (mth to last), it should return 7. The method should do by making only a single pass through the list as suggested in the reading. This method should have time complexity O(n) and space complexity O(1) (no additional space) where n is the length of the linked list.
Problem 2
For this problem, we're back to using arrays (not linked lists). A Java program that takes an integer array and removes another set of integers from the array, returning the new length. For example, suppose:
int x[] = {5, 3, 2, 1, 2, 6, 7} int remove[] = {2, 6, 5}
int removeInts(x, remove) would change x to be {3, 1, 7} and return a new length of 3. Notice that the order of elements in x is preserved. There should not be any "empty" slots in x.
Part
without using additional storage (storage complexity of O(1)) and minimal time complexity.
Since you cannot shorten the length of an array, have a variable track the new "length" of x.
Show your program working on the above example by including screenshots.
Let n = the length of array x, and r = the length of array 'remove'. What is the time complexity of your solution in terms of n and r?
Part B
Solve the problem again, but this time you can use additional storage.removeIntsFaster(x, remove). What is the time complexity for your new solution?