# 单链表排序

```package link;
public class SingleLinkNode {
private int data;
this.data = data;
this.nextNode = nextNode;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public SingleLinkNode getNextNode() {
return nextNode;
}
public void setNextNode(SingleLinkNode nextNode) {
this.nextNode = nextNode;
}
}```

```package link;
import java.util.Random;
public class SingleLinkList {
// the header node of the single link list

}
// construct the single link list which contains 10 nodes
for(int i=0;i<9;i++){
currentNode.setNextNode(nextNode);
currentNode=nextNode;
}
}
public String toString(){
String result="";
while(currentNode!=null){
result+=currentNode.getData()+"-->";
currentNode=currentNode.getNextNode();
}
result+="null";
return result;
}
}
```

排序算法类Utils.java

```package link;
public class Utils {
// base on bubble sort
if(nextNode==null){
}

int tempData=-1;
// if the last node is the second node,the loop is over
// check whether the next node is the last node.
// if yes, then ship this loop.
if(nextNode==lastNode){
lastNode=currentNode;
nextNode=currentNode.getNextNode();
continue;
}
if(currentNode.getData()>nextNode.getData()){
// exchange the data
tempData=currentNode.getData();
currentNode.setData(nextNode.getData());
nextNode.setData(tempData);
}
currentNode=currentNode.getNextNode();
nextNode=currentNode.getNextNode();
}
}
}```

• 0

开心

• 0

板砖

• 0

感动

• 0

有用

• 0

疑问

• 0

难过

• 0

无聊

• 0

震惊