标签:
/* * 281. Zigzag Iterator * 2016-6-27 by Mingyang * 这里就利用了list里面的Iterator的性质,不断地交替取出Iterator,并且取下一个值 */ class ZigzagIterator { LinkedList<Iterator> list; public ZigzagIterator(List<Integer> v1, List<Integer> v2) { list = new LinkedList<Iterator>(); if(!v1.isEmpty()) list.add(v1.iterator()); if(!v2.isEmpty()) list.add(v2.iterator()); } public int next() { Iterator poll = list.remove(); int result = (Integer)poll.next(); if(poll.hasNext()) list.add(poll); return result; } public boolean hasNext() { return !list.isEmpty(); } }
标签:
原文地址:http://www.cnblogs.com/zmyvszk/p/5622494.html