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
  • 相关阅读:
    数组对象
    禁止鼠标右键保存图片、文字,禁止拖动图片等代码
    解决vscode 电脑卡顿
    vscode Html标签自动补全
    git提交报错
    作为一个程序员为什么要写博客?
    aaa
    JDBC
    去ioe
    去中心化
  • 原文地址:https://www.cnblogs.com/echoht/p/3680629.html
Copyright © 2011-2022 走看看