码迷,mamicode.com
首页 > 其他好文 > 详细

【Leetcode】Insertion Sort List

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

标签:blog   io   for   div   amp   log   new   ad   

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

标签:blog   io   for   div   amp   log   new   ad   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!