The answer below has runtime error,but I couldn't find the problem,I need some help


  • 0
    L

    class Solution {
    public:
    ListNode* insertionSortList(ListNode* head) {
    ListNode* newHead=new ListNode(0);
    newHead->next=head;
    ListNode* pre=newHead;
    ListNode* cur=head;
    while(cur){
    if(cur->next&&cur->next->val<cur->val){
    while(pre->next&&pre->next->val<cur->next->val)
    pre=pre->next;
    ListNode* temp=cur->next;
    cur->next=cur->next->next;
    cur->next->next=pre->next;
    pre->next=temp;

                pre=newHead;
            }
            else cur=cur->next;
        }
        return newHead->next;
    }
    

    };


  • 0
    Z

    More people will help you if you can edit it in a correct format, I think.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.