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();
        // TODO
        return 0;
    }

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

@@ -387,15 +387,557 @@
     
-// TODO
+Integer last = null;%0A        boolean delete = true;%0A        boolean asc = true;%0A        while (iterator.hasNext()) %7B%0A            while (iterator.hasNext()) %7B%0A                last = iterator.next();%0A                if (delete) %7B%0A                    iterator.remove();%0A                %7D%0A                delete = !delete;%0A            %7D%0A            if (asc) %7B%0A                iterator = list.descendingIterator();%0A            %7D else %7B%0A                iterator = list.iterator();%0A            %7D%0A            asc = !asc;%0A            delete = true;%0A        %7D
 %0A   
@@ -948,17 +948,20 @@
  return 
-0
+last
 ;%0A    %7D%0A
@@ -1106,16 +1106,198 @@
 ); // 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%0A
     %7D%0A%7D%0A

Update problem 390