您的位置:首页 > 其它

LeetCode Insertion Sort List

2016-03-02 10:51 330 查看
/**
* 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* dummy = new ListNode(-1);//dummy node;
ListNode* cur = head;//node to be inserted
ListNode* pre=dummy;//node will be inserted between pre and pre->next;
ListNode* next ;//cur->next
while(cur!=NULL){
next=cur->next;
while(pre->next!=NULL&&(pre->next->val<=cur->val)) pre=pre->next;
cur->next = pre->next;
pre->next = cur;
cur=next;
pre=dummy;
}
return dummy->next;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: