Skip to content

Commit 90b1d30

Browse files
authored
Update 281. Zigzag Iterator.java
general case implementation
1 parent add6a8d commit 90b1d30

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

281. Zigzag Iterator.java

+22
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ public ZigzagIterator(List<Integer> v1, List<Integer> v2) {
2626
index = 0;
2727
}
2828

29+
/**
30+
* General case (not tested): List of Lists
31+
public ZigzagIterator(List<List<Integer>> v) {
32+
zzList = new ArrayList<Integer>();
33+
for (int i = 0; i < v.size(); i++) {
34+
length += v.get(i).size();
35+
}
36+
index = 0; // keep track of number of elements in total
37+
int order = 0; // keep track of which list to remove next
38+
39+
while (index < length) {
40+
while (v.get(order).size() == 0) {
41+
order++;
42+
}
43+
zzList.add(v.get(order).remove(0));
44+
order++;
45+
index++;
46+
}
47+
index = 0;
48+
}
49+
*/
50+
2951
public int next() {
3052
return zzList.get(index++);
3153
}

0 commit comments

Comments
 (0)