-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathZigzagIterator.java
More file actions
55 lines (49 loc) · 1.19 KB
/
Copy pathZigzagIterator.java
File metadata and controls
55 lines (49 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import java.util.List;
public class ZigzagIterator
{
private final List<Integer> list1;
private final List<Integer> list2;
int list1Index = 0;
int list2Index = 0;
int currentList = -1;
public ZigzagIterator(List<Integer> v1, List<Integer> v2)
{
this.list1 = v1;
this.list2 = v2;
currentList = !v1.isEmpty() ? 1 : !v2.isEmpty() ? 2 : -1;
}
public int next()
{
if (currentList == 1)
{
int value = list1.get(list1Index++);
if (list2Index == list2.size())
{
currentList = list1Index == list1.size() ? -1 : 1;
}
else
{
currentList = 2;
}
return value;
}
if (currentList == 2)
{
int value = list2.get(list2Index++);
if (list1Index == list1.size())
{
currentList = list2Index == list2.size() ? -1 : 2;
}
else
{
currentList = 1;
}
return value;
}
return -1;
}
public boolean hasNext()
{
return currentList != -1;
}
}