public class MergeList { private int head; private MergeList tail; public MergeList(int head, MergeList tail) { this.head = head; this.tail = tail; } private MergeList append(MergeList other) { if (tail == null) return new MergeList(head,other); else return new MergeList(head,tail.append(other)); } private MergeList reverseAcc(MergeList acc) { if (tail == null) return new MergeList(head,acc); else return tail.reverseAcc(new MergeList(head,acc)); } private MergeList reverse() { if (tail == null) return this; else return tail.reverse().append(new MergeList(head,null)); } private MergeList merge(MergeList other) { if (other == null) return this; else if (head <= other.head) if (tail != null) return new MergeList(head,tail.merge(other)); else return new MergeList(head,other); else return new MergeList(other.head,merge(other.tail)); } }