fork(3) download
  1.  
  2. import java.util.*;
  3.  
  4. class Ideone {
  5. public static void main (String[] args) {
  6. List<Integer> list = new ArrayList<>(
  7. Arrays.asList(6, 4, 5, 6, 0, 6, 3, 4, 1, 6, 1, 6, 0, 6, 8, 3));
  8. List<Integer> subList = Arrays.asList(6, 0, 6);
  9.  
  10. removeAllSubList(list, subList);
  11. System.out.println(list);
  12. }
  13.  
  14. public static void removeAllSubList(List<?> list, List<?> subList) {
  15. // find first occurrence of the subList in the list, O(nm)
  16. int i = Collections.indexOfSubList(list, subList);
  17. // if found
  18. if (i != -1) {
  19. // bulk remove, O(m)
  20. list.subList(i, i + subList.size()).clear();
  21. // recurse with the rest of the list
  22. removeAllSubList(list.subList(i, list.size()), subList);
  23. }
  24. }
  25. }
Success #stdin #stdout 0.05s 711168KB
stdin
Standard input is empty
stdout
[6, 4, 5, 3, 4, 1, 6, 1, 8, 3]