私のLinkedList -
class MyList{
int N;
MyList next = null;
MyList(int N){
this.N = N;
}
@Override
public String toString() {
MyList curr = this;
String output = "";
while(curr != null){
output = output+curr.N+"-->";
curr = curr.next;
}
return output+"TAIL";
}
}
ソート方法アルゴリズム
private static MyList sortLL(MyList L){
int temp;
if(L == null || L.next == null)
return L;
MyList current = L;
MyList previous = null;
while(current.next != null){
if(current.N > current.next.N){
temp = current.N;
current.N = current.next.N;
current.next.N = temp;
}
previous = current;
current = current.next;
}
return previous;
}
入力 -
MyList list_Sort = new MyList(9);
list_Sort.next = new MyList(8);
list_Sort.next.next = new MyList(8);
list_Sort.next.next.next = new MyList(7);
list_Sort.next.next.next.next = new MyList(5);
list_Sort.next.next.next.next.next = new MyList(4);
list_Sort.next.next.next.next.next.next = new MyList(6);
list_Sort.next.next.next.next.next.next.next = new MyList(3);
list_Sort.next.next.next.next.next.next.next.next = new MyList(1);
list_Sort.next.next.next.next.next.next.next.next.next = new MyList(2);
入力 - 9-->8-->8-->7-->5-->4-->6-->3-->1-->2-->TAIL
出力 - 2-->9-->TAIL
期待される出力 - 入力はソートされている必要があります