链表插入排序

链表插入排序
题目描述:用插入排序法对链表进行排序
given 1->3->2->0->NULL, return 0->1->2->3->NULL
算法描述:
算法一:将题目中给出的链表数据结构转换为C++标准库中的标准list数据结构,调用c++标准库中的list算法进行排序即可完成
算法二:将题目中给出的链表数据结构转换为vrector,对vector进行插入排序,插入排序的算法如下所示:

    vector<int> insertSort(vector<int> a, int length)
    {
        int key,i;
        for(int j = 1;j<length;j++)
        {
            key = a[j];
            i = j;
            while(i>0 && a[i-1] > key)
            {
                a[i] = a[i-1];
                i = i-1;
            }
            a[i] = key;
        }
        return a;
    }

插入排序的整体思想类似于玩扑克牌游戏摸牌阶段整理扑克牌顺序的思想。再对排序好的vector创建为list。创建list的代码如下:

    ListNode* createList(vector<int>a, int length)
    {
        ListNode * head, *tail, *mid;
        head = tail = NULL;
        int i = 0;
        while (i < length)
        {
            mid = new ListNode(-1);
            mid->val = a[i];
            mid->next = NULL;
            if (head == NULL)
            {
                head = mid;
            }
            else
            {
                tail->next = mid;
            }
            tail = mid;
            i++;
        }
        return head;
    }

则解决该问题的整体代码如下:

/**
 * Definition of ListNode
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *         this->val = val;
 *         this->next = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * @param head: The first node of linked list.
     * @return: The head of linked list.
     */
    ListNode *insertionSortList(ListNode *head)
    {
        // write your code here
        //step one: List 2 vector
        vector<int> raw_data;
        vector<int> mid_data;
        ListNode* result;
        while(head != NULL)
        {
            raw_data.push_back(head->val);
            head = head ->next;
        }
        //step two: sorting
        mid_data = insertSort(raw_data,raw_data.size());
        //step three: vector 2 list
        result = createList(mid_data,raw_data.size());
        return result;    
    }
    vector<int> insertSort(vector<int> a, int length)
    {
        int key,i;
        for(int j = 1;j<length;j++)
        {
            key = a[j];
            i = j;
            while(i>0 && a[i-1] > key)
            {
                a[i] = a[i-1];
                i = i-1;
            }
            a[i] = key;
        }
        return a;
    }
    ListNode* createList(vector<int>a, int length)
    {
        ListNode * head, *tail, *mid;
        head = tail = NULL;
        int i = 0;
        while (i < length)
        {
            mid = new ListNode(-1);
            mid->val = a[i];
            mid->next = NULL;
            if (head == NULL)
            {
                head = mid;
            }
            else
            {
                tail->next = mid;
            }
            tail = mid;
            i++;
        }
        return head;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值