首页 > 其他 > 详细

【Leetcode】Insertion Sort List

时间:2014-08-20 14:03:52      阅读:198      评论:0      收藏:0      [点我收藏+]

Sort a linked list using insertion sort.

 

1st ( 3 tries)

/**
 * 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 *cur;
        ListNode *pre;
        if(head == NULL)
            return NULL;
        if(head->next == NULL)
            return head;
        pre = head;
        cur = head->next;
        while(cur != NULL)
        {
            //scan from the head
            if(cur->val < pre->val)
            {
                ListNode *innercur = head->next;
                ListNode *innerpre = head;
                while(cur->val > innercur->val)
                {
                    innercur = innercur->next;
                    innerpre = innerpre->next;
                }
                if(innerpre == head && innerpre->val > cur->val)
                {
                    pre->next = cur->next;
                    cur->next = head;
                    head = cur;
                }
                else
                {
                    pre->next = cur->next;
                    innerpre->next = cur;
                    cur->next = innercur;
                }
                cur = pre->next;
            }
            else
            {
                pre = pre->next;
                cur = cur->next;
            }
        }
        return head;
    }
};

  

2nd ( 5 tries)

/**
 * 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) {
        //insertion sort mean that element before i must be sorted
        ListNode *newhead = new ListNode(INT_MIN);
        newhead->next = head;
        ListNode *spre = newhead;
        ListNode *scur = head;
        //change scur!!!
        while(scur != NULL) {
            ListNode *pre = newhead;
            ListNode *cur = newhead->next;
            while(cur != scur && cur->val <= scur->val) {
                pre = pre->next;
                cur = cur->next;
            }
            //change
            if(cur != scur) {
                spre->next = scur->next;
                pre->next = scur;
                scur->next = cur;
                
                scur = spre->next;
            }
            else {
                spre = spre->next;
                scur = scur->next;
            }
        }
        ListNode *ans = newhead->next;
        delete newhead;
        return ans;
    }
};

  

【Leetcode】Insertion Sort List,布布扣,bubuko.com

【Leetcode】Insertion Sort List

原文:http://www.cnblogs.com/weixliu/p/3924377.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!