zoukankan      html  css  js  c++  java
  • leetcode 147. Insertion Sort List ----- java

     

    Sort a linked list using insertion sort.

    插入排序。

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode insertionSortList(ListNode head) {
            
            if( head == null )
                return null;
        
            ListNode node = head.next;
            head.next = null;
            ListNode start = head;
    
            while( no sdf sd sdfnull ){
                ListNode nn = node.next;
                ListNode first = start;
                if( node.val < first.val){
                    node.next = first;
                    start = node;
                }else{
                    while( first.next != null && first.next.val < node.val )
                        first = first.next;
                    node.next = first.next;
                    first.next = node;
    
                }
                
                node = nn;
    
            }
            return start;
            
        }
    }
  • 相关阅读:
    AOP
    关于zookeeper部署的个数
    Zookeeper 简介
    Java 正则表达式
    面试记录
    面试题
    Spring Framework官方文档翻译(中英文版)
    java知识巩固
    mysql sql记录
    redis入门
  • 原文地址:https://www.cnblogs.com/xiaoba1203/p/6070196.html
Copyright © 2011-2022 走看看