zoukankan      html  css  js  c++  java
  • java 直接插入排序


    package wangChaoPA实习工作练习.com.leetcode;
    public class InsertList
    {
        public ListNode insertionSortList(ListNode head)
        {
            if (head == null || head.next == null)
            {
                return head;
            }
            //头结点
            ListNode dummy = new ListNode(-1);
            //断开链表
            dummy.next = null;
            ListNode newNext, pre, next;
            while (head != null)
            {
                //需要进行插入的下一个结点
                next = head.next;
                //获取到插入的前结点
                pre = searchForIsertPos(dummy, head.val);
                //newNext=null
                newNext = pre.next;
                //断开链表
                head.next = newNext;
                //前面排好序的链表进行连接
                pre.next = head;
                //head指向下一个需要插入的结点
                head = next;
            }
            //返回首结点(头结点指向首结点)
            return dummy.next;
        }

        // 获取到插入的前结点
        public ListNode searchForIsertPos(ListNode head, int val)
        {
            ListNode pre = head;
            ListNode next = head.next;
            while (next != null && next.val <= val)
            {
                pre = next;
                next = next.next;
            }
            return pre;
        }
    }
    /**
     *
     * <p>
     * 描述该类情况 {@link 代表跟谁有关系}
     * </p>
     *
     * @author 王超
     * @since 1.0
     * @date 2017年5月11日 下午11:05:37
     * @see 新建|修改|放弃
     * @see wangChaoPA实习工作练习.com.leetcode.InsertList 插入排序算法的实现:插入一个结点 前面的都是排好序的 从小到大
     *      对链表进行插入排序的正确方法是:新建一个头节点,遍历原来的链表,对原链表的每个节点找到新链表中适合插入位置的前指针,然后执行插入操作。
     *      这种操作链表的题有一个技巧就是新建一个dummy作为head,然后把数据插入到dummy的链表中,最后返回dummy.next。
     */
    class ListNode1
    {
        ListNode1 next;
        int val;

        ListNode1(int x)
        {
            this.val = x;
            this.next = null;
        }
    }

  • 相关阅读:
    mysql中delimiter
    error: unpacking of archive failed on file /usr/sbin/zabbix_agent;592e5bc3: cpio: open
    CefSharp中文帮助文档
    ASP.NET Aries 开发框架
    简洁的富文本编辑器
    asp.net core 获取appsettings.json里的配置
    在asp.net core中使用NLog
    临时禁用Resharper
    visual studio 无添加视图 选项
    visual studio(vs)初始化
  • 原文地址:https://www.cnblogs.com/qingtianBKY/p/6853760.html
Copyright © 2011-2022 走看看