`
cozilla
  • 浏览: 89369 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

sort link list

阅读更多

题目描述

对一个单链表原地(in-place)排序。即直接对链表结点排序。返回排序后链表的头结点。

链表结点的定义为(请不要在代码中再次定义该结构):

C/C++
struct ListNode {
    int val;
    ListNode *next;
}

渣做法!N^2

ListNode* insert(ListNode* head, ListNode* item) {
	if (item == NULL) return head;
    if (head == NULL) { item->next = NULL; return item; }
	if (item->val < head->val) { item->next = head; return item;}
	ListNode* prev = head, *cur = head->next;
	while (cur != NULL) {
		if (cur->val < item->val) prev = cur, cur = cur->next;
		else {
			prev->next = item;
			item->next = cur;
			return head;
		}
	}

	if (cur == NULL) { 
		prev->next = item;
		item->next = NULL;
	}
	return head;
}
ListNode* sortLinkList(ListNode *head) {
	if (!head) return head;

	ListNode *cur = head, *newhead = NULL, *next;
	while (cur != NULL) {
		next = cur->next;
		newhead = insert(newhead, cur);
		cur = next;
	}
	return newhead;
}

 

 

 merge sort

ListNode* merge(ListNode* a, ListNode* b) {
	if (a == NULL || b == NULL) return a == NULL ? b : a;
	ListNode* head, *cur;
	if (a->val < b->val) head = a, a = a->next;
	else head = b, b = b->next;
	head->next = NULL;
	cur = head;
	while (a != NULL && b != NULL) {
		if (a->val < b->val) cur->next = a, cur = cur->next, a = a->next;
		else cur->next = b, cur = cur->next, b = b->next;
	}
	if (a != NULL) cur->next = a;
	else if (b != NULL) cur->next = b;
	else cur->next = NULL;
	return head;
}

ListNode* mergeSort(ListNode* start, int len) {
	if (len <= 0) return NULL;
	if (len == 1) return start;
	int mid = len / 2, t;
	ListNode *midnode = start;
	t = mid;
	while (t-- > 1) midnode = midnode->next;
	ListNode *start2 = midnode->next;
	midnode->next = NULL;
	return merge(mergeSort(start, mid), mergeSort(start2, len - mid));
}

ListNode* sortLinkList(ListNode *head) {
	ListNode* cur = head;
	int n = 0;
	while (cur != NULL) n++, cur = cur->next;
	return mergeSort(head, n);
}

 

 

 

分享到:
评论

相关推荐

    link_list_learn_内核链表_exactly42h_learnc_学习_populations4s_

    链表学习记录:new_stumanager.c 学生信息管理系统list_sort.c list_sort.h:双向循环内核链表冒泡排序,节点位置交换。list_sort.c list_sort.h:单向内核链表冒泡排序,节点位置交换。sort_arr:数组冒泡排序

    连接两个链表c语言

    }}void selection_sort(link pointer,int num){ link tmp,btmp; int i,min; for(i=0;i;i++) { tmp=pointer; min=tmp-&gt;data; btmp=NULL; while(tmp-&gt;next) { if(min&gt;tmp-&gt;next-&gt;data) {min=tmp-&gt;next-&gt;data;...

    MDPI 爬取 ‘title_link’, ‘author_list’, ‘cited_by’, ‘viewed_by’ Demo 数据保存至CSV文件

    https://www.mdpi.com/search?sort=article_citedby&page_no=0&page_count=50&year_from=1996&year_to=2020&journal=cells&view=default Page : Demo : # encoding: utf-8 @author: lanxiaofang @contact: fang...

    OutlookAttachView v2.73

    the Extensions List, Excluded Extensions List, Subject contains string..., From string, and To string, and you can easily select a string again from a Combo-Box. * Version 2.47 o Added option to ...

    Sortable前端框架

    sort: true, // sorting inside list delay: 0, // time in milliseconds to define when the sorting should start touchStartThreshold: 0, // px, how many pixels the point should move before cancelling a...

    使用MVC5的Entity Framework 教程源码

    ViewBag.CurrentSort = sortOrder; ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date"; if (searchString != ...

    MyBase Desktop 6.0.2 破解正式版(官方更新至10/10/2011)

    8.Added: the 'Organize' menu onto the main menu, as many users can't find the 'Sort child items' utility, which is also located in the 'Outline' action menu. 9.Added: Alt-Drag to create symbolic links...

    HA-Xenu-yfy

    Xenu's Link Sleuth (TM) checks Web sites for broken links.... It displays a continously updated list of URLs which you can sort by different criteria. A report can be produced at any time.

    python实现的各种排序算法代码

    #选择排序def select_sort(sort_array): for i, elem in enumerate(sort_array): for j, elem in enumerate(sort_array[i:]): if sort_array[i] &gt; sort_array[j + i]: #交换 sort_array[i],

    DOCman_Ultimate_module_1.5.zip

    - &lt;param name="link" type="list" default="details" label="Link" description="Where the document link should go"&gt; &lt;option value="details"&gt;Details&lt;/option&gt; &lt;option value="download"&gt;Download&lt;/option&gt; ...

    Linux下的rar解压缩工具

    ds Disable name sort for solid archive dw Wipe files after archiving e[+]&lt;attr&gt; Set file exclude and include attributes ed Do not add empty directories en Do not put 'end of archive' block ep ...

    leetcode卡-leetCode_exercise:leetCode_exercise

    leetcode卡面试准备+ Leetcode Leetcode相关 array stack link list heap tree search sort graph 字卡笔记 tags:就活

    DiskControls_XE5

    Supports big number of features such like automatical sorting of the list items with arrow-style sort mark on the header section, individual context menus for every shell object, possibility to hide ...

    《数据结构 1800题》

    《数据结构 1800题》 第一章 绪论 一、选择题 1. 算法的计算量的大小称为计算的(B )。【北京邮电大学2000 二、3 (20/8分)】 A.效率 B....2. 算法的时间复杂度取决于(C )【中科院计算所 1998 二、1 (2分)...

    opengoo 1.3 RC1

    - usability: Sort weblinks by date as well as name. - usability: Improved display of documents' contents. HTML documents are shown with its CSS. Long documents are shown with a scrollbar, so that it...

    PHP jqGrid 数据网格显示并分页

    jqGrid 是一个用来显示网格数据的jQuery插件,文档比较全面,附带中文... sortorder: "desc", caption:"JSON 实例" }); jQuery("#list2").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false}); &lt;/script&gt;

    Python3 菜鸟查询手册

    09.13 列表方法 List sort()方法.png 09.14 列表方法 List clear()方法.png 09.15 列表方法 List copy()方法.png 10 元组.png 11 字典.png 11.01 字典 clear()方法.png 11.02 字典 copy()方法.png 11.02.01 ...

    苹果8XPC和手机二合一完整版

    [link:name]名称,支持长度控制[link:name len=10] [link:link]地址 [link:pic]图片 {/maccms:link} ****************************友情链接标签 开始**************************** ***************************...

    linux常用命令源码(ls,cp,chmod,df等一百多个命令)

    group-list.c groups.c head.c hostid.c hostname.c id.c install.c join.c kill.c lbracket.c libstdbuf.c link.c ln.c logname.c ls.c ls-dir.c ls-ls.c ls-vdir.c md5sum.c mkdir.c mkfifo.c mknod.c mktemp.c mv...

    Dreamweaver CS4 黄金插件10-1

    57. DwZone Dynamic Sort Table V1.1.0 For Adobe Dreamweaver 58. DwZone Multi Column Combo Box V1.1.7 For Adobe Dreamweaver 59. DwZone Professional Query Manager V1.5.4 For Adobe Dreamweaver 60. DwZone ...

Global site tag (gtag.js) - Google Analytics