首页 > 编程语言 > 详细

单链表的排序

时间:2021-05-23 09:12:04      阅读:19      评论:0      收藏:0      [点我收藏+]

题目:给定一个无序单链表,实现单链表的排序(按升序排序)

思路:

代码:

 1 /*
 2  * function ListNode(x){
 3  *   this.val = x;
 4  *   this.next = null;
 5  * }
 6  */
 7 
 8 /**
 9  * 
10  * @param head ListNode类 the head node
11  * @return ListNode类
12  */
13 function sortInList( head ) {
14     // write code here
15     let p = head;
16     const arr = [];
17     while(p){
18         arr.push(p.val);
19         p = p.next;
20     }
21     arr.sort ((a,b)=> a-b);
22     p = head;
23     let i = 0;
24     while(p){
25         p.val = arr[i++];
26         p = p.next;
27     }
28     return head;
29 }
30 module.exports = {
31     sortInList : sortInList
32 };

 

单链表的排序

原文:https://www.cnblogs.com/icyyyy/p/14800333.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!