import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
public ListNode sortInList (ListNode head) {
if (head == null) {
return head;
}
ListNode curNode = head;
int tempNodeVal = 0;
while (curNode != null) {
//当前节点的下一个节点,因为要从当前节点的后一节点开始 循环遍历出小于temp的 然后和首位置节点的val交换
/**
* 内重循环从当前节点的下一个节点循环到尾节点,
* 找到和外重循环的值比较最小的那个,然后与外重循环进行交换
*/
ListNode nextNode = curNode.next;
//从nextNode节点开始循环遍历出 后续节点中最小的那个节点的val
while (nextNode != null) {
//每次循环以curNode的val作为链表最小值进行比较
if (curNode.val > nextNode.val) {
tempNodeVal = curNode.val;
curNode.val = nextNode.val;
nextNode.val = tempNodeVal;
}
nextNode = nextNode.next;
}
curNode = curNode.next;
}
return head;
}
}