Question is from here.
Today’s exercise provides three little exercises on linked lists, designed to help beginning programmers learn more about how lists work; there is also a special invitation for more experienced programmers.
- Write a function that takes an input list and an interval n and returns a new list with all the elements of the original list, in order, except that every nth item has been removed. For instance, given the input list (1 2 3 4 5 6 7 8 9 10) and n = 4, the function should return the list (1 2 3 5 6 7 9 10).
- Write a function that takes an input list and returns a new list with all the elements of the original list, in order, except that in the case of duplicate elements all of the duplicates except the first has been removed. For instance, all of the following lists should be transformed into the list (1 2 3 4 5): (1 2 3 4 5), (1 1 2 3 4 5), (1 2 1 3 1 4 1 5 1), and (1 2 2 3 3 3 4 4 4 4 5 5 5 5 5).
- Write a function that takes an input list and splits the list in half; for instance, given the input list (1 2 3 4 5) the two outputs are the lists (1 2) and (3 4 5). If the list has odd length the middle element can be placed in either half, at your option, so the lists (1 2 3) and (4 5) are an alternate acceptable solution for the example problem.
Solution – Exercise 1:
import java.util.*;
public class Three_List_Exercises_1 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9));
System.out.println(reShape(list, 4));
}
private static List<Integer> reShape(List<Integer> list, int n) {
int count = 1;
Iterator<Integer> it = list.iterator();
List<Integer> res = new ArrayList<Integer>();
int current_n = n;
while(it.hasNext()) {
int c = it.next();
if(count < current_n)
res.add(c);
else if(count == current_n)
current_n += n;
count++;
}
return res;
}
}
Output:
[1, 2, 3, 5, 6, 7, 9]
Solution – Exercise 2:
import java.util.*;
public class Three_List_Exercises_2 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,1,3,1,4,1,5,1));
System.out.println(removeDuplicate(list));
}
private static List<Integer> removeDuplicate(List<Integer> list) {
Set<Integer> set = new LinkedHashSet<Integer>();
List<Integer> res = new ArrayList<Integer>();
Iterator<Integer> it = list.iterator();
while(it.hasNext()) {
set.add(it.next());
}
res.addAll(set);
return res;
}
}
Output:
[1, 2, 3, 4, 5]
Solution – Exercise 3:
import java.util.*;
public class Three_List_Exercises_3 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
split(list);
}
private static void split(List<Integer> list) {
int mid = list.size()/2;
List<Integer> first = new ArrayList<Integer>();
List<Integer> second = new ArrayList<Integer>();
int count = 0;
Iterator<Integer> it = list.iterator();
while(it.hasNext()) {
if(count++ < mid)
first.add(it.next());
else
second.add(it.next());
}
System.out.println("f: " + first);
System.out.println("s: " + second);
}
}
Output:
f: [1, 2] s: [3, 4, 5]