package leetcode;

import java.util.Iterator;
import java.util.LinkedList;

/**
 * https://leetcode.com/problems/elimination-game/
 */
public class Problem390 {
    public int lastRemaining(int n) {
        LinkedList<Integer> list = new LinkedList<>();
        for (int i = 1; i <= n; i++) {
            list.add(i);
        }
        Iterator<Integer> iterator = list.iterator();
        Integer last = null;
        boolean delete = true;
        boolean asc = true;
        while (iterator.hasNext()) {
            while (iterator.hasNext()) {
                last = iterator.next();
                if (delete) {
                    iterator.remove();
                }
                delete = !delete;
            }
            if (asc) {
                iterator = list.descendingIterator();
            } else {
                iterator = list.iterator();
            }
            asc = !asc;
            delete = true;
        }
        return last;
    }

    public static void main(String[] args) {
        Problem390 prob = new Problem390();
        System.out.println(prob.lastRemaining(9)); // 6
        System.out.println(prob.lastRemaining(10)); // 8
        System.out.println(prob.lastRemaining(5)); // 2
        System.out.println(prob.lastRemaining(10000000)); // 6150102
    }
}

@@ -16,65 +16,8 @@
 e;%0A%0A
-import java.util.Iterator;%0Aimport java.util.LinkedList;%0A%0A
 /**%0A
@@ -147,181 +147,43 @@
     
-L
 in
-kedList%3CInteger%3E list = new LinkedList%3C%3E();%0A        for (int i = 1; i %3C= n; i++) %7B%0A            list.add(i);%0A        %7D%0A        Iterator%3CInteger%3E iterator = list.iterator()
+t first = 1;%0A        int last = n
 ;%0A  
@@ -192,27 +192,17 @@
     
-Integer last = null
+int m = 1
 ;%0A  
@@ -219,125 +219,65 @@
 ean 
-delete = true;%0A        boolean asc = true;%0A        while (iterator.hasNext()) %7B%0A            while (iterator.hasNext()
+left = true;%0A        while (n %3E 1) %7B%0A            if (left
 ) %7B%0A
@@ -296,30 +296,18 @@
     
-la
+fir
 st 
++
 = 
-iterator.next()
+m
 ;%0A  
@@ -324,22 +324,26 @@
     if (
-delete
+n %25 2 != 0
 ) %7B%0A    
@@ -362,25 +362,17 @@
     
-iterator.remove()
+last -= m
 ;%0A  
@@ -403,186 +403,124 @@
     
-    delete = !delete;%0A            %7D%0A            if (asc) %7B%0A                iterator = list.descendingIterator();%0A            %7D else %7B%0A                iterator = list.iterator();%0A
+%7D else if (!left) %7B%0A                last -= m;%0A                if (n %25 2 != 0) %7B%0A                    first += m;%0A   
     
@@ -519,32 +519,33 @@
 %0A               
+ 
 %7D%0A            as
@@ -546,19 +546,9 @@
     
-asc = !asc;
+%7D
 %0A   
@@ -560,387 +560,109 @@
     
-delete = true;%0A        %7D%0A        return last;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem390 prob = new Problem390();%0A        System.out.println(prob.lastRemaining(9)); // 6%0A        System.out.println(prob.lastRemaining(10)); // 8%0A        System.out.println(prob.lastRemaining(5)); // 2%0A        System.out.println(prob.lastRemaining(10000000)); // 6150102
+left = !left;%0A            n /= 2;%0A            m *= 2;%0A        %7D%0A        return (left) ? last : first;
 %0A   

Solve problem 390