如何在数组中输入元素然后遍历它们

时间:2016-05-04 16:53:41

标签: java sorting linked-list

如何在数组中输入元素然后遍历它们?

1 个答案:

答案 0 :(得分:0)

public LinkedList<T> sort() {
    return sort(this.getNext());
}

public LinkedList<T> sort(LinkedList<T> head) {
    if (head == null) {
        this.setNext(null);
        return this;
    }

    sortedHead = head.sort();
    if (this.getData().compareTo(sortedHead.getData()) < 0) {
        this.setNext(sortedHead);
        return this;
    } else {
        sortedHead.setNext(this.sort(sortedHead.getNext()));
        return sortedHead;
    }
}