题解 | 单链表的排序

单链表的排序

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;

    }

全部评论
感觉代码没问题,就是超时了,23个样例能跑通20个
点赞 回复 分享
发布于 01-08 20:50 陕西

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务