Insert Sort Singly List

对单链表插入排序,给出个单链表的head节点;返回排完序的head节点;

首先数据结构中习惯了以数组为参数排序,瞬间想到是遍历单链表存入arraylist中,再进行insert sort,(O(n**2)),space(O(n)),leetcode过不去;

链表插入排序注意事项:

1:依次调用head.next的循环结束条件  listNode.next ==null;

2: 插入可能出现的情况a) 以排序的链表前面 b)中间,c)tail。

3:分类讨论,

4:以排完序的链表和未排完序链表之间的引用(指针next)

时间: 2024-08-25 17:48:37

Insert Sort Singly List的相关文章

计数排序(Count Sort )与插入排序(Insert Sort)

计数排序法:计数数组适用于当前数组密集的情况.例如(2,3,5,4,2,3,3,2,5,4) 方法:先找出最大值最小值,之后统计每个数出现的次数,根据次数从小到大往数组里添加 计数排序法是一种不需要比较的排序方法 1 void count(int top,int length,int arr[]) 2 { 3 int min=arr[0],max=arr[0],i=1,j=0; 4 int *count=(int*)malloc(sizeof(int)*(max-min+1)); 5 if(ar

Shell Sort based on insert sort

Reason:when it comes to insert sort,the snail appears,as it just moves forward step by step or even worse.So we need some improvement.the first idea may be that we can walk forward in big strides.Shell sort do the same thing. the basic realization(fo

leetcode:Insert Sort List

问题描述 对一个单链表进行插入排序,head指向第一个结点. 代码 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *insertionSortList(ListNode *head) { if(!h

CLRS:Insert sort in in c

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<time.h>#define ARRAY_SIZE 1000int buf[ARRAY_SIZE];int main(){ int i,j,n; srand((unsigned int)time(0)); memset(buf,0,sizeof(buf[0])); while(scanf("%d",&n)!=

insert sort 插入排序

#pragma once #include <algorithm> #include <list> template<typename T> void InsertSort(list<T>& container) { std::list<T>::iterator it_min = container.begin(); std::list<T>::iterator it_temp; std::list<T>::ite

Sort Integers

Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort or any O(n2) algorithm. 分析 bubble sort 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class Solution {     /**      * @param A an integer a

Sort in Swift

var unnumber = [23,76,42,9,7,0,1,37,4,2,89,23,45,56,87,65,20,15,23,15,17] var number = [19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0] var number2 = [19,18] var number1 = [19] //to swap two number func swapValue(inout left:Int, inout _ right:Int)

各种sort

冒泡排序 Bubble Sort  O(N^2) 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.这步做完后,最后的元素会是最大的数. 针对所有的元素重复以上的步骤,除了最后一个. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较. 伪代码 function bubble_sort (array, length) { var i, j; for(i from 0 to length-1){ for(j fro

sort algorithm

insert sort: merge sort: 其实很像后续遍历. package com.java2novice.sorting; public class MyMergeSort { private int[] array; private int[] tempMergArr; private int length; public static void main(String a[]){ int[] inputArr = {45,23,11,89,77,98,4,28,65,43}; M