博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode-Insertion Sort List[AC源码]
阅读量:6924 次
发布时间:2019-06-27

本文共 1556 字,大约阅读时间需要 5 分钟。

1 package com.lw.leet5; 2  3 /** 4  * @ClassName:Solution 5  * @Description: 6  *         Insertion Sort List  7  *         Sort a linked list using insertion sort. 8  * @Author LiuWei 9  * @Date 2014年8月20日下午7:50:0710  * @Mail nashiyue1314@163.com 11  */12 public class Solution {13 14     public ListNode insertionSortList(ListNode head) {15         // if only one or two elements ,return head16         if(head == null || head.next == null){17             return head;18         }19         ListNode cur = head.next;20         ListNode tmp = null;21         while(cur != null){22             tmp = head;23             //get the insertion position24             while(tmp != cur && tmp.val < cur.val){25                 tmp = tmp.next;26             }27             //if need insert, switch the value28             if(tmp != cur){29                 int num1 = cur.val;30                 int num2 ;31                 while(tmp != cur){32                     //store the tmp value33                     num2 = tmp.val;34                     tmp.val = num1;35                     num1 = num2;36                     tmp = tmp.next;37                 }38                 //init cur39                 tmp.val = num1;40             }41             cur = cur.next;42         }43         return head;44     }45 46     public static void main(String args[]) {47         int[] arr = {10,3,2,4,0,0,-1,2,-2};48          ListNode head = new ListNode(arr[0]);49          ListNode curr = head;50          for(int i=1; i

 

转载于:https://www.cnblogs.com/nashiyue/p/3925603.html

你可能感兴趣的文章
opencv基础知识------IplImage, CvMat, Mat 的关系和相互转换
查看>>
邪淫的六种情况
查看>>
Spring中注解事务方面的问题
查看>>
[SQL]死锁处理语句
查看>>
临别前夕,工作总结 于2014年8月13日 终稿
查看>>
【floyd】HDU 1874 畅通project续
查看>>
第十篇:扩展SOUI的控件及绘图对象(ISkinObj)
查看>>
Winform开发框架中实现信息阅读状态的显示和存储
查看>>
Android 下拉刷新框架实现
查看>>
IGS_学习笔记09_IREP生成服务后台工具Soagenerate.sh
查看>>
安卓开发_浅谈ListView之分页列表
查看>>
斐波那契堆
查看>>
mongodb batchInsert
查看>>
Sun公司开源游戏服务器
查看>>
Jasmine测试ng Promises - Provide and Spy
查看>>
Response.Write具体介绍
查看>>
ARM汇编编程基础之一 —— 寄存器
查看>>
安装LVS安装LVS和配置LVS的工作比较繁杂
查看>>
Scilab 的画图函数(1)
查看>>
经常使用的android弹出对话框
查看>>