您的位置:首页 > 其它

[leetcode]Insertion Sort List

2014-04-11 14:56 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) {
// if there is only one node or null, return immediately
if(head==NULL || head->next == NULL) return head;
// insert each node from second node to the front part
ListNode * current = head->next;
head->next = NULL;
while(current) {
ListNode * temp = head;
ListNode * prev = NULL;
// to keep the next node of current node to be inserted, important!!!
ListNode * next = current->next;
while(temp){
if(current->val > temp->val) {
// do not find the right place to insert
prev = temp;
temp = temp->next;
} else {
// find the right place to insert
if(prev == NULL){
// if it is the head node
current->next = head;
head = current;
} else {
current->next = temp;
prev->next = current;
}
break;
}
}
// if it is the end node
if(!temp) {
prev->next = current;
current->next = NULL;
}
current = next;
}
return head;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息