@@ -0,0 +1,2650 @@
+package coding4fun;%0A%0Aimport java.util.Arrays;%0Aimport java.util.Iterator;%0Aimport java.util.List;%0A%0A/**%0A * Given two 1d list, implement an iterator to return their elements alternately.%0A * For example, given two 1d lists:%0A *%0A * %3Cpre%3E%0A * list1 = %5B1, 2%5D%0A * list2 = %5B3, 4, 5, 6%5D%0A * %3C/pre%3E%0A *%0A * By calling next repeatedly until hasNext returns false, the order of elements returned%0A * by next should be: %5B1, 3, 2, 4, 5, 6%5D.%0A */%0Apublic class Problem26 %7B%0A    public static class ZigZagIterator implements Iterator%3CInteger%3E %7B%0A        private final Iterator%3CInteger%3E iter1;%0A        private final Iterator%3CInteger%3E iter2;%0A        private boolean first = true;%0A%0A        public ZigZagIterator(List%3CInteger%3E list1, List%3CInteger%3E list2) %7B%0A            iter1 = list1.iterator();%0A            iter2 = list2.iterator();%0A%0A            if (list1.isEmpty()) %7B%0A                first = false;%0A            %7D%0A        %7D%0A%0A        @Override%0A        public boolean hasNext() %7B%0A            if (first) %7B%0A                return iter1.hasNext();%0A            %7D%0A            return iter2.hasNext();%0A        %7D%0A%0A        @Override%0A        public Integer next() %7B%0A            if (first) %7B%0A                if (iter1.hasNext()) %7B%0A                    Integer elem = iter1.next();%0A                    if (iter2.hasNext()) %7B%0A                        first = !first;%0A                    %7D%0A                    return elem;%0A                %7D%0A            %7D else %7B%0A                if (iter2.hasNext()) %7B%0A                    Integer elem = iter2.next();%0A                    if (iter1.hasNext()) %7B%0A                        first = !first;%0A                    %7D%0A                    return elem;%0A                %7D%0A            %7D%0A            return null;%0A        %7D%0A    %7D%0A%0A    private static void print(ZigZagIterator iter) %7B%0A        while (iter.hasNext()) %7B%0A            System.out.print(iter.next() + %22 %22);%0A        %7D%0A        System.out.println();%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        List%3CInteger%3E list1 = Arrays.asList(1, 2);%0A        List%3CInteger%3E list2 = Arrays.asList(3, 4, 5, 6);%0A%0A        ZigZagIterator iter = new ZigZagIterator(list1, list2);%0A        print(iter);%0A%0A        iter = new ZigZagIterator(list2, list1);%0A        print(iter);%0A%0A        list1 = Arrays.asList(1, 2, 3);%0A        list2 = Arrays.asList(4, 5, 6);%0A%0A        iter = new ZigZagIterator(list1, list2);%0A        print(iter);%0A%0A        iter = new ZigZagIterator(list2, list1);%0A        print(iter);%0A%0A        list1 = Arrays.asList();%0A        list2 = Arrays.asList(4, 5, 6);%0A%0A        iter = new ZigZagIterator(list1, list2);%0A        print(iter);%0A%0A        iter = new ZigZagIterator(list2, list1);%0A        print(iter);%0A    %7D%0A%7D%0A

Add problem 26