LeetCode(147) Insertion Sort List

题目如下:

Sort a linked list using insertion sort.

分析如下:

使用插入排序的方法,对一个链表进行排序。看上去题目比较简单。
不过坑还是有几个的:
1 要记得把排序好的新链表和待排序的旧链表断开,也就是排序好的新链表的尾节点要赋值NULL。否则最后就乱了。
2 什么叫做插入排序,从A[1]到A[n],依次把旧链表中的旧元素放入新链表的正确位置中,新链表是从没有元素开始逐渐排好序的,每加入一个旧元素,就把它放入正确的位置中。有三种可能的位置,新链表的表头,新链表的表中,新链表的表尾,因为在不同的位置会造成对head和tail指针做不同的处理,所以需要分情况讨论。

我的代码:

// 280ms
/**
 * 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(head==NULL||head->next==NULL) //bug1:特殊情况处理。
            return head;
        ListNode* cur=head;
        ListNode* next=head;
        ListNode* new_head=NULL;
        ListNode* new_tail=NULL;
        while(cur!=NULL){
            next=cur->next;
            if(new_head==NULL){ //如果新链表的头结点为空
                new_head=cur;
                new_tail=cur;
                new_tail->next=NULL;
            }else{
                ListNode* tmp_pre=NULL;
                ListNode* tmp_cur=new_head;
                while(tmp_cur!=NULL&&cur->val>tmp_cur->val){ //bug2:注意不能少了判断语句tmp_cur!=NULL
                        tmp_pre=tmp_cur;
                        tmp_cur=tmp_cur->next;
                }
                if(tmp_pre==NULL){//假如旧元素放入新链表的表头
                    cur->next=new_head;
                    new_head=cur; 
                }else if(tmp_cur==NULL){ //就如旧元素放入新链表的表尾
                    new_tail->next=cur;
                    new_tail=cur;
                    new_tail->next=NULL; //bug3:不要忘了tail节点还要用NULL表示节点
                }else{//假如旧元素放入新链表的表中
                    cur->next=tmp_cur;
                    tmp_pre->next=cur;
                }
            }
            cur=next;
        }
        return new_head;
    }
};


小结:

(1) 一开始悲剧了,把插入排序想错了,想成了每次都从链表结尾取旧元素,提交发现超时了,修改之后又debug了各种错误,终于通过了。

(2) 其实我觉得本题最重要的地方是,用链表做插入排序的时候,新旧链表必须用NULL分割开,否则出错。但是在用数组做插入排序的时候,这样的问题是不存在的。

(3) 有道面试题,让你对一个长度未知的链表进行排序,使用O(1)的空间复杂度。最合适的解决方案就是插入排序了。


update: 2014-12-30

// 248ms
/**
 * 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 (head == NULL) return head;
        ListNode* new_head = new ListNode(INT_MIN);
        ListNode* current = head;
        ListNode* next = head;
        ListNode* insert_cur = new_head;
        ListNode* insert_pre = new_head;
        int length = 0;
        while (current != NULL) {
            ++length;
            current = current->next;
        }
        for (int i = 0; i < length ; ++i) {//逐个遍历旧链表中的每个节点,将它们生成到新链表中。
            current = next;
            next = current->next;
            insert_cur = new_head;
            while (insert_cur!= NULL && current->val >= insert_cur->val) {//找寻在新链表中的插入位置。
                insert_pre =  insert_cur;
                insert_cur = insert_cur->next;
            }
            if (insert_cur != NULL) {//如果不在新链表的tail
                current->next = insert_cur;
                insert_pre->next = current;
            } else {//如果在新链表的tail,要NULL收尾。
                insert_pre->next = current;
                current->next = NULL;
            }
        }
        return new_head->next;
    }
};


注意上面的, Line 24  这里是从i = 0到length而不是length - 1,因为这里需要逐个遍历链表的每个节点,和数组的插入排序不一样。

还要注意上面的, Line 28  注意上面是">="不是">",">"逻辑错误。另外,如果上面是">" 不是">="的话[-2147483248, -2147483247] 失败。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值