Suppose I have a list like this:
[4, 5, 7, 2, 3, 1, 9]
I want the traversal for it to be like this:
4, 9, 5, 1, 7, 3, 2
Can someone share a solution for this? The solution can be language agnostic - even pseudocode is fine.
One solution I have in mind is to make a doubly ended queue and dequeue elements from alternate ends. But the problem is I want to traverse this list multiple times and dequeuing will remove those elements for future use. Is there any other solution that can avoid me from making a copy of the queue each time before traversing it in my desired order?
How about:
i = 0;
j = arr.size() - 1;
while(i < j){
print(arr[i++]);
print(arr[j--]);
}
// center still needs to be printed
if(i == j){
print(arr[i]);
}