zoukankan      html  css  js  c++  java
  • 【LeetCode】Insertion Sort List

    Sort a linked list using insertion sort.

    //用到O(N)的额外空间
    public class Solution {
        public ListNode insertionSortList(ListNode head) {
            if(head==null||head.next==null)
                return head;
            ListNode root = new ListNode(head.val);
            ListNode cur = head.next;
            while(cur!=null){
                ListNode tempNode = root;
                ListNode pre = root;
                while(tempNode!=null){
                    if(cur.val>tempNode.val){
                        pre=tempNode;
                        tempNode=tempNode.next;
                    }else{
                        break;
                    }
                }           
                if(tempNode==root){
                    ListNode newNode = new ListNode(cur.val);
                    newNode.next=root;
                    root=newNode;
                    cur=cur.next;
                }else if(tempNode==null){
                    ListNode newNode = new ListNode(cur.val);
                    pre.next=newNode;
                    cur=cur.next;
                }else{
                    ListNode newNode = new ListNode(cur.val);
                    pre.next=newNode;
                    newNode.next=tempNode;
                    cur=cur.next;
                }
                
                
            }
            
            return root;
            
        }
    }
    public class NSolution {
        public ListNode insertionSortList(ListNode head) {
            if(head==null||head.next==null)
                return head;
            
            ListNode cur = head.next;
            head.next=null;
            while(cur!=null){
                ListNode tempNode = head;
                ListNode pre = head;
                while(tempNode!=null){
                    if(cur.val>tempNode.val){
                        pre=tempNode;
                        tempNode=tempNode.next;
                    }else{
                        break;
                    }
                }           
                if(tempNode==head){
                    ListNode newNode = new ListNode(cur.val);
                    newNode.next=head;
                    head=newNode;
                    cur=cur.next;
                    
                    
                    
                }else if(tempNode==null){
                    ListNode newNode = new ListNode(cur.val);
                    pre.next=newNode;
                    cur=cur.next;
                }else{
                    ListNode newNode = new ListNode(cur.val);
                    pre.next=newNode;
                    newNode.next=tempNode;
                    cur=cur.next;
                }
                
                
            }
            
            return head;
            
        }
    }
  • 相关阅读:
    Redis配置文件的使用
    WEB请求处理一:浏览器请求发起处理
    Nginx配置文件(nginx.conf)配置详解
    【node】------mongoose的基本使用
    Promise.resolve()与new Promise(r => r(v))
    promise是什么?
    apiDoc
    apiDoc 使用指南
    微信小程序-性能与体验优化
    微信小程序-调取上一页的方法
  • 原文地址:https://www.cnblogs.com/yixianyixian/p/3721685.html
Copyright © 2011-2022 走看看