sealed interface List permits LinkedElem, Elem {} public record LinkedElem(T a, List l) implements List {} public record Elem(T a) implements List {} public class SwitchAppend { public append(l1, l2) { return switch(l1) { case LinkedElem(a, Elem(e)) -> new LinkedElem<>(a, new LinkedElem<>(e, l2)); case LinkedElem(a, r) -> new LinkedElem<>(a, append(r, l2)); //Alternativ: //case LinkedElem(a, LinkedElem(e, r)) -> new LinkedElem<>(a, append(new LinkedElem(e, r), l2)); //default -> null; }; } }