/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
List list2
= Arrays.
asList("1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10"); List list3
= Arrays.
asList("1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"11");
arraysList(list2);
linkedList(list2);
arraysList(list3);
linkedList(list3);
}
private static void linkedList
(List list
){ LinkedList<String> in = new LinkedList<String>(list);
LinkedList<String> out = new LinkedList<String>();
out.addFirst(in.getFirst());
Iterator<String> it = in.iterator();
it.next();
Iterator<String> rit = in.descendingIterator();
rit.next();
int median = in.size() / 2 - 1;
for (int i = 0; i < median; i++) {
out.add(rit.next());
out.add(it.next());
}
if (in.size() % 2 == 1) {
out.addLast(it.next());
}
out.addLast(in.getLast());
System.
out.
println("LinkedList: "+out
+ ".size("+out.
size()+")"); }
private static void arraysList
(List list
) { ArrayList<String> in = new ArrayList<String>(list);
ArrayList<String> out = new ArrayList<String>(in.size());
out.add(in.get(0));
int last = in.size() - 1;
int median = in.size() / 2 ;
for (int i = 1; i < median ; i++) {
String r
= in.
get(in.
size() - i
- 1); out.add(r);
out.add(l);
}
if (in.size() % 2 == 1) {
out.add(in.size() - 2, in.get(median));
}
out.add(last, in.get(last));
System.
out.
println("ArrayList : "+out
+ ".size("+out.
size()+")"); }
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB0aH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AgICB9CgogICAgaWYgKGluLnNpemUoKSAlIDIgPT0gMSkgewogICAgICAgIG91dC5hZGRMYXN0KGl0Lm5leHQoKSk7CiAgICB9CgogICAgb3V0LmFkZExhc3QoaW4uZ2V0TGFzdCgpKTsKICAgIFN5c3RlbS5vdXQucHJpbnRsbigiTGlua2VkTGlzdDogIitvdXQgKyAiLnNpemUoIitvdXQuc2l6ZSgpKyIpIik7Cn0KCiAgICBwcml2YXRlIHN0YXRpYyB2b2lkIGFycmF5c0xpc3QoTGlzdCBsaXN0KSB7CiAgICAgICAgQXJyYXlMaXN0PFN0cmluZz4gaW4gID0gbmV3IEFycmF5TGlzdDxTdHJpbmc+KGxpc3QpOwogICAgICAgIEFycmF5TGlzdDxTdHJpbmc+IG91dCA9IG5ldyBBcnJheUxpc3Q8U3RyaW5nPihpbi5zaXplKCkpOyAKICAgICAgICAKICAgICAgICBvdXQuYWRkKGluLmdldCgwKSk7CiAgICAgICAgaW50IGxhc3QgPSBpbi5zaXplKCkgLSAxOwogICAgICAgCiAgICAgICAgaW50IG1lZGlhbiA9ICBpbi5zaXplKCkgLyAyIDsKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8IG1lZGlhbiA7IGkrKykgewogICAgICAgICAgICBTdHJpbmcgciA9IGluLmdldChpbi5zaXplKCkgLSBpIC0gMSk7CiAgICAgICAgICAgIFN0cmluZyBsID0gaW4uZ2V0KGkpOwogICAgICAgICAgICBvdXQuYWRkKHIpOwogICAgICAgICAgICBvdXQuYWRkKGwpOwogICAgICAgIH0KICAgICAgCiAgICAgICAgaWYgKGluLnNpemUoKSAlIDIgPT0gMSkgewogICAgICAgICAgICAgICAgb3V0LmFkZChpbi5zaXplKCkgLSAyLCBpbi5nZXQobWVkaWFuKSk7CiAgICAgICAgfQogICAgICAgICBvdXQuYWRkKGxhc3QsIGluLmdldChsYXN0KSk7IAoKICAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJBcnJheUxpc3QgOiAiK291dCArICIuc2l6ZSgiK291dC5zaXplKCkrIikiKTsKICAgIH0KCQp9
ArrayList : [1, 9, 2, 8, 3, 7, 4, 6, 5, 10].size(10)
LinkedList: [1, 9, 2, 8, 3, 7, 4, 6, 5, 10].size(10)
ArrayList : [1, 10, 2, 9, 3, 8, 4, 7, 5, 6, 11].size(11)
LinkedList: [1, 10, 2, 9, 3, 8, 4, 7, 5, 6, 11].size(11)