Advertisement
korobushk

Iterator<>

Mar 18th, 2021 (edited)
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1.         LinkedList<Integer> list = new LinkedList<>(); //add 0 1 2 3 4 5 6
  2.        
  3.           Iterator<Integer> it = list.iterator(); // so this is used with collections
  4.    
  5.     // we need to remove the k item from the list if k = 3  we remove list[2] after list[5] after [1]
  6.    
  7. //circular?
  8.  
  9.  // makes it go k times to the next item in the list
  10.  while (count < k) {  
  11.                   while (it.hasNext() && count < k) {   // 0      ||
  12.                     it.next();          //0 1 2                 ||
  13.                     count++;            //1 2 3   == 3          ||
  14.                 }
  15.              it.remove();  // remove list[2] ||
  16. //remove k=3
  17.  
  18.  
  19. //if count < k we need to restart the it from 0 (make a new it with same list?)
  20.      // restart counter to 0 always until , work until list size is 1 , output only 1 number
  21.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement