题解 | 单链表的排序
单链表的排序
https://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08?tpId=295&tqId=1008897&sourceUrl=%2Fexam%2Foj%3FquestionJobId%3D10%26subTabName%3Donline_coding_page
ListNode* sortInList(ListNode* head) {
// write code here
if(!head || !head->next) return head;
ListNode* pre=head;
ListNode* cur=nullptr;
while(pre){
cur=pre->next;
while(cur){
if(pre->val>cur->val){
int temp=pre->val;
pre->val=cur->val;
cur->val=temp;
}
cur=cur->next;
}
pre=pre->next;
}
return head;
}
