首页 > 代码库 > Insertion Sort List

Insertion Sort List

不知道为什么{3,4,1}

就是通不过。

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode *insertionSortList(ListNode *head) {                ListNode *nextnode;        ListNode *p;        ListNode *t;        ListNode *preNode=NULL;        for(p=head;p!=NULL;p=p->next)        {            nextnode=p->next;            if(nextnode==NULL) continue;                         for(t=head;t!=p->next&&nextnode->val > t->val;t=t->next)            {                preNode=t;            }                if(preNode!=NULL){                    if(preNode==p);                    else                    {                    p->next=nextnode->next;                    nextnode->next=preNode->next;                    preNode->next=nextnode;                    }                }            else            {                p->next=nextnode->next;                nextnode->next=head;                head=nextnode;            }            preNode==NULL;        }        return head;    }};