zoukankan      html  css  js  c++  java
  • leetcode[147]Insertion Sort List

    Sort a linked list using insertion sort.

    /**
     * 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 *newh=new ListNode(0);
            ListNode *p=head;
            while(p)
            {
                ListNode *pretmp=newh;
                ListNode *pnext=p->next;
                ListNode *tmp=newh->next;
                if(!tmp)
                {
                    newh->next=p;
                    newh->next->next=NULL;
                    p=pnext;
                    continue;
                }
                while(tmp)
                {
                    if(p->val<=tmp->val)
                    {
                        pretmp->next=p;
                        p->next=tmp;
                        break;
                    }
                    else
                    {
                        pretmp=tmp;
                        tmp=tmp->next;
                    }
                }
                if(!tmp)
                {
                    pretmp->next=p;
                    pretmp->next->next=NULL;
                }
                p=pnext;
            }
            return newh->next;
        }
    };
  • 相关阅读:
    cp
    usr/sbin/inetd
    mysql
    Iptables的规则语法
    CentOS系统安装过程中配置软RAID-0或RAID-1
    25道shell面试题
    虚拟机
    进入单用户模式
    正则表达式
    js操作div的显隐
  • 原文地址:https://www.cnblogs.com/Vae1990Silence/p/4281215.html
Copyright © 2011-2022 走看看