Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Solution4 {
- public static void main(String[] args) {
- LinkedList<String> myList = new LinkedList<>(List.of("z", "z", "b", "a", "c", "c", "l"));
- ListIterator<String> listIterator = myList.listIterator();
- int count = 1;
- listIterator.next();
- while (listIterator.hasNext()) {
- String ch = listIterator.next();
- listIterator.previous();
- if (ch.equals(listIterator.previous())) {
- count++;
- listIterator.next();
- listIterator.next();
- listIterator.remove();
- } else {
- listIterator.next();
- if(count != 1) { // an thes na emfanizei kai to 1 vgale tin if i kanto true
- listIterator.add(String.valueOf(count));
- }
- listIterator.next();
- count = 1;
- }
- }
- if (count != 1) listIterator.add(String.valueOf(count)); // an thes na emfanizei kai to 1 vgale tin if i kanto true
- myList.forEach(x -> System.out.print(x + " "));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement