我的链表 -
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
预期输出 - 输入应按排序顺序