您的位置:首页 > 其它

leetcode - Insertion Sort List

2013-11-20 14:41 281 查看
/**
* 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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if (head == NULL || head->next == NULL)
return head;
ListNode * itr =head->next;
head->next = NULL;
while (itr != NULL){
ListNode * node = head, * last =NULL;
ListNode *next = itr->next;
while ((node != NULL) && (node->val < itr->val)){
last = node;
node = node->next;
}
itr->next = node;
if (node == head)
head = itr;
else
last->next = itr;
itr = next;
}
return head;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: