zoukankan      html  css  js  c++  java
  • LeetCode OJ

    这道题较简单,这里不多说,下面是AC的代码:

     1 /**
     2      * 插入排序的算法比较简单,我看wiki上,当要在已排序列表中插入一个元素时,是从后到前遍历,
     3      * 由于链表只能从前往后遍历,所以我采用从后到前。
     4      * 要注意的地方就是找到位置时,处理链表的元素插入的具体细节
     5      * @param head
     6      * @return
     7      */
     8     public ListNode insertionSortList(ListNode head){
     9         //当链表为空或者只有一个节点的情况
    10         if(head == null || head.next == null)
    11             return head;
    12         
    13         ListNode newHead = head;//记录新的链表头
    14         ListNode p1 = head, pp1= null, pointer,pPointer = head, temp = null;
    15         for( pointer = head.next;pointer!=null;pointer = temp)
    16         {
    17             pp1 = null;
    18             p1 = newHead;
    19             while(p1!= pointer && p1.val<=pointer.val)
    20             {
    21                 pp1 = p1;
    22                 p1 = p1.next;
    23             }
    24             temp = pointer.next;
    25             
    26             if(pp1 == null)
    27             {
    28                 pointer.next = p1;
    29                 newHead = pointer;
    30                 pPointer.next = temp;
    31             }else if(p1 != pointer){
    32                 pPointer.next = pointer.next;
    33                 pp1.next = pointer;
    34                 pointer.next = p1;
    35             }
    36             else
    37                 pPointer = pPointer.next;
    38             
    39         }
    40         return newHead;
    41     }
    有问题可以和我联系,bettyting2010#163 dot com
  • 相关阅读:
    How does Android, PHP, SQL, JSON, and Remote Databases work together?
    Gson Json
    Protocol Android
    AsyncTask、多线程及线程通信
    线程-Android
    HTTP请求-Android
    Make Your First Android App
    Git版本控制软件结合GitHub从入门到精通常用命令学习手册
    正向代理设置
    vscode 调试 react 项目
  • 原文地址:https://www.cnblogs.com/echoht/p/3680629.html
Copyright © 2011-2022 走看看