给定一个节点数为n的无序单链表,对其按升序排序。
数据范围:0 < n≤100000
冒泡算法:
public class Solution {
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
public ListNode sortInList (ListNode head) {
ListNode cur = head;
ListNode nextNode = null;
int temp = 0;
while (cur.next != null) {
nextNode = cur.next;
while (nextNode != null) {
if (cur.val > nextNode.val) {
temp = cur.val;
cur.val = nextNode.val;
nextNode.val = temp;
}
nextNode = nextNode.next;
}
cur = cur.next;
}return head;
}
}