Advertisement
Guest User

FinalSolution

a guest
Mar 25th, 2020
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.15 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Solution4 {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         LinkedList<String> myList = new LinkedList<>(List.of("z", "z", "b", "a", "c", "c", "l"));
  8.         ListIterator<String> listIterator = myList.listIterator();
  9.        
  10.         int count = 1;
  11.  
  12.         listIterator.next();
  13.  
  14.         while (listIterator.hasNext()) {
  15.  
  16.             String ch = listIterator.next();
  17.             listIterator.previous();
  18.  
  19.             if (ch.equals(listIterator.previous())) {
  20.                 count++;
  21.                 listIterator.next();
  22.                 listIterator.next();
  23.                 listIterator.remove();
  24.             } else {
  25.                 listIterator.next();
  26.                 if(count != 1) { // an thes na emfanizei kai to 1 vgale tin if i kanto true
  27.                     listIterator.add(String.valueOf(count));
  28.                 }
  29.                 listIterator.next();
  30.                 count = 1;
  31.             }
  32.  
  33.         }
  34.         if (count != 1) listIterator.add(String.valueOf(count)); // an thes na emfanizei kai to 1 vgale tin if i kanto true
  35.         myList.forEach(x -> System.out.print(x + " "));
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement