日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Insertion Sort List

系統 1973 0

這題一開始想新建一個list,結果MLE了,后來想了想不用新建,insertion的概念理解好就行,具體編程部分不難

      
         1
      
      
        /*
      
      
        *


      
      
         2
      
      
         * Definition for singly-linked list.


      
      
         3
      
      
         * struct ListNode {


      
      
         4
      
      
         *     int val;


      
      
         5
      
      
         *     ListNode *next;


      
      
         6
      
      
         *     ListNode(int x) : val(x), next(NULL) {}


      
      
         7
      
      
         * };


      
      
         8
      
      
        */
      
      
         9
      
      
        class
      
      
         Solution {


      
      
        10
      
      
        public
      
      
        :


      
      
        11
      
           ListNode *insertionSortList(ListNode *
      
        head) {


      
      
        12
      
      
        if
      
       (!head) 
      
        return
      
      
         NULL;


      
      
        13
      
               ListNode *hpre = 
      
        new
      
       ListNode(
      
        0
      
      
        );


      
      
        14
      
               hpre->next =
      
         head;


      
      
        15
      
               ListNode *pre =
      
         head;


      
      
        16
      
               head = head->
      
        next;


      
      
        17
      
      
        while
      
      
         (head) {


      
      
        18
      
      
        if
      
       (head->val > pre->
      
        val) {


      
      
        19
      
                       head = head->
      
        next;


      
      
        20
      
                       pre = pre->
      
        next;


      
      
        21
      
      
                    }


      
      
        22
      
      
        else
      
      
         {


      
      
        23
      
                       pre->next = head->
      
        next;


      
      
        24
      
                       ListNode *tmp =
      
         hpre;


      
      
        25
      
      
        while
      
       (head->val > tmp->next->val) tmp = tmp->
      
        next;


      
      
        26
      
                       head->next = tmp->
      
        next;


      
      
        27
      
                       tmp->next =
      
         head;


      
      
        28
      
                       head = pre->
      
        next;


      
      
        29
      
      
                    }


      
      
        30
      
      
                }


      
      
        31
      
      
        return
      
       hpre->
      
        next;


      
      
        32
      
      
            }


      
      
        33
      
       };
    

?C#

        
           1
        
        
          /*
        
        
          *


        
        
           2
        
        
           * Definition for singly-linked list.


        
        
           3
        
        
           * public class ListNode {


        
        
           4
        
        
           *     public int val;


        
        
           5
        
        
           *     public ListNode next;


        
        
           6
        
        
           *     public ListNode(int x) { val = x; }


        
        
           7
        
        
           * }


        
        
           8
        
        
          */
        
        
           9
        
        
          public
        
        
          class
        
        
           Solution {


        
        
          10
        
        
          public
        
        
           ListNode InsertionSortList(ListNode head) {


        
        
          11
        
        
          if
        
         (head == 
        
          null
        
        ) 
        
          return
        
        
          null
        
        
          ;


        
        
          12
        
                 ListNode hpre = 
        
          new
        
         ListNode(
        
          0
        
        
          );


        
        
          13
        
                 hpre.next =
        
           head;


        
        
          14
        
                 ListNode pre =
        
           head;


        
        
          15
        
                 head =
        
           head.next;


        
        
          16
        
        
          while
        
         (head != 
        
          null
        
        
          ) {


        
        
          17
        
        
          if
        
         (head.val >
        
           pre.val) {


        
        
          18
        
                         head =
        
           head.next;


        
        
          19
        
                         pre =
        
           pre.next;


        
        
          20
        
        
                      }


        
        
          21
        
        
          else
        
        
           {


        
        
          22
        
                         pre.next =
        
           head.next;


        
        
          23
        
                         ListNode tmp =
        
           hpre;


        
        
          24
        
        
          while
        
         (head.val > tmp.next.val) tmp =
        
           tmp.next;


        
        
          25
        
                         head.next =
        
           tmp.next;


        
        
          26
        
                         tmp.next =
        
           head;


        
        
          27
        
                         head =
        
           pre.next;


        
        
          28
        
        
                      }


        
        
          29
        
        
                  }


        
        
          30
        
        
          return
        
        
           hpre.next;


        
        
          31
        
        
              }


        
        
          32
        
         }
      
View Code

?

Insertion Sort List


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 永丰县| 武陟县| 兰坪| 扎囊县| 喀什市| 思南县| 井陉县| 油尖旺区| 五寨县| 武川县| 灵璧县| 乌拉特中旗| 平山县| 武宁县| 科技| 贺兰县| 老河口市| 富锦市| 兴义市| 定结县| 科技| 江陵县| 浪卡子县| 古田县| 东兰县| 游戏| 尼玛县| 翁牛特旗| 濮阳县| 上饶市| 武城县| 中西区| 石嘴山市| 奎屯市| 台东市| 若尔盖县| 玉门市| 阳江市| 荥阳市| 安宁市| 浙江省|