题目描述
对一个单链表原地(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); }
相关推荐
链表学习记录:new_stumanager.c 学生信息管理系统list_sort.c list_sort.h:双向循环内核链表冒泡排序,节点位置交换。list_sort.c list_sort.h:单向内核链表冒泡排序,节点位置交换。sort_arr:数组冒泡排序
}}void selection_sort(link pointer,int num){ link tmp,btmp; int i,min; for(i=0;i;i++) { tmp=pointer; min=tmp->data; btmp=NULL; while(tmp->next) { if(min>tmp->next->data) {min=tmp->next->data;...
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...
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 ...
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...
ViewBag.CurrentSort = sortOrder; ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date"; if (searchString != ...
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...
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.
#选择排序def select_sort(sort_array): for i, elem in enumerate(sort_array): for j, elem in enumerate(sort_array[i:]): if sort_array[i] > sort_array[j + i]: #交换 sort_array[i],
- <param name="link" type="list" default="details" label="Link" description="Where the document link should go"> <option value="details">Details</option> <option value="download">Download</option> ...
ds Disable name sort for solid archive dw Wipe files after archiving e[+]<attr> Set file exclude and include attributes ed Do not add empty directories en Do not put 'end of archive' block ep ...
leetcode卡面试准备+ Leetcode Leetcode相关 array stack link list heap tree search sort graph 字卡笔记 tags:就活
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题》 第一章 绪论 一、选择题 1. 算法的计算量的大小称为计算的(B )。【北京邮电大学2000 二、3 (20/8分)】 A.效率 B....2. 算法的时间复杂度取决于(C )【中科院计算所 1998 二、1 (2分)...
- 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...
jqGrid 是一个用来显示网格数据的jQuery插件,文档比较全面,附带中文... sortorder: "desc", caption:"JSON 实例" }); jQuery("#list2").jqGrid('navGrid','#pager2',{edit:false,add:false,del:false}); </script>
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 ...
[link:name]名称,支持长度控制[link:name len=10] [link:link]地址 [link:pic]图片 {/maccms:link} ****************************友情链接标签 开始**************************** ***************************...
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...
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 ...