您的位置:首页 > 其它

LeetCode OJ Insertion Sort List

2015-03-23 00:39 375 查看
Sort a linked list using insertion sort.

class Solution {
public:
    ListNode *insertionSortList(ListNode *head) {
        for (ListNode *temp_1 = head; temp_1 != NULL; temp_1 = temp_1->next) {
            int min = temp_1->val;
            ListNode *remem = temp_1;
            for (ListNode *temp_2 = temp_1->next; temp_2 != NULL; temp_2 = temp_2->next) {
                if (temp_2->val < min) {
                    min = temp_2->val;
                    remem = temp_2;
                }
            }
            remem->val = temp_1->val;
            temp_1->val = min;
        }
        return head;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: