Insertion Sort List

系统 1774 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条评论