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(!head||head->next==NULL)
			return head;
		ListNode *pstart=new ListNode(0);//添加一个头指针以方便处理,设所有节点数据大于0
		pstart->next=head;
		ListNode *preCur=head,*cur=head->next;
		while(cur!=NULL)
		{
		    ListNode *prePos=pstart,* pos=pstart->next;
			while(pos->val<cur->val)
			{
				prePos=prePos->next;//prePos指向带插入位置的前方
				pos=pos->next;//pos指向待插入位置的后方
			}
			if(pos!=cur)
			{
				preCur->next=cur->next;
				cur->next=pos;
				prePos->next=cur;
				//preCur不变
				cur=preCur->next;
			}
			else
			{
				preCur=cur;
				cur=cur->next;
			}
		}
		head=pstart->next;
		delete pstart;
		return head;
	}
};

时间复杂度

O(N^2),相对于顺序存储减少移动次数。

leetcode:Insert Sort List

时间: 2024-11-02 12:32:24

leetcode:Insert Sort List的相关文章

[leetcode]Insert Interval @ Python

原题地址:https://oj.leetcode.com/problems/insert-interval/ 题意: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times

LeetCode: Insertion Sort List [147]

[题目] Sort a linked list using insertion sort. [题意] 用插入排序方法排序链表 [思路] 直接搞 [代码] /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode

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:以排完序的链表和未排完序链表之间

LeetCode Insertion Sort List

class Solution { public: ListNode *insertionSortList(ListNode *head) { if (head == NULL) return NULL; ListNode* sorted_head = head; ListNode* unsorted_head = head->next; head->next = NULL; ListNode* cur = unsorted_head; while (cur != NULL) { unsorte

计数排序(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

【LeetCode】Sort Colors

LeetCode OJ Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, w

[leetcode]Insertion Sort List @ Python

原题地址:http://oj.leetcode.com/problems/insertion-sort-list/ 题意:对链表进行插入排序. 解题思路:首先来对插入排序有一个直观的认识,来自维基百科. 代码循环部分图示: 代码: class Solution: # @param head, a ListNode # @return a ListNode def insertionSortList(self, head): if not head: return head dummy = Lis

leetcode——Insertion Sort List 对链表进行插入排序(AC)

Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode *result; result->val = INT_MIN; result->next = NULL; ListNode *cur=head,*

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