题解 | #单链表的排序#
单链表的排序
https://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08
package main import ( . "nc_tools" "sort" ) /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 the head node * @return ListNode类 */ func sortInList( head *ListNode ) *ListNode { // write code here var( nums []int h *ListNode = head ) for ;head != nil; { nums = append(nums, head.Val) head = head.Next } sort.Ints(nums) head = h for i := range nums { head.Val = nums[i] head = head.Next } return h }