zoukankan      html  css  js  c++  java
  • 链表 插入排序

    /**注意往链表头插入元素的情况
     * 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)) return head;
            ListNode *res=head;
            head = head->next;
            res->next=NULL;
           
            while(head){
                ListNode *p=res;
                while(p->next && (p->next->val) <= (head->val)){
                   p=p->next;
                }
                if((p->val) > (head->val)){                  //这几个要注意顺序
                    ListNode *temp = head->next;
                    head->next=p;
                    res=head;
                    head=temp;
                    
                }
                else{                                          //注意顺序
                
                ListNode *temp = p->next;
                p->next=head;
                head=head->next;
                p->next->next=temp;
                }
                
                }
            return res;
            }
    };
  • 相关阅读:
    echarts 饼图
    vue echarts
    :style :class
    弹框弹出时禁止页面滚动
    2019-2-10 日记
    2019-1-27 日记
    2019-1-26 日记
    2019-1-3 日记
    2019-1-10 日记
    2019-1-2 日记
  • 原文地址:https://www.cnblogs.com/julie-yang/p/4665463.html
Copyright © 2011-2022 走看看