如何在自身内部引用LinkedList

时间:2015-06-30 23:34:12

标签: java sorting linked-list

我正在尝试通过填写新的临时列表来对LinkedList进行排序。 它工作正常 - ' tempList'排序正确 - 但我如何引用其中的实际列表?

public class LinkedList implements AbstractListType {

    public void addSorted(Object data){
        /* */
    }

    public void sort(){
        Node runpointer = first;
        AbstractListType tempList = new LinkedList();

        while(runpointer.next != null){
            tempList.addSorted(runpointer.data);
            runpointer = runpointer.next;
        }
    // tempList == this list //
    }        

1 个答案:

答案 0 :(得分:0)

public class LinkedList implements AbstractListType {

public void addSorted(Object data){
    /* */
}

public void sort(){
    Node runpointer = first;
    AbstractListType tempList = new LinkedList();

    while(runpointer.next != null){
        tempList.addSorted(runpointer.data);
        runpointer = runpointer.next;
    }
   runpointer.head = tempList.head;   // now your runpointer has the same list in the tempList
}