题解 | #单链表的排序#
单链表的排序
https://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08
using System; using System.Collections.Generic; /* public class ListNode { public int val; public ListNode next; public ListNode (int x) { val = x; } } */ class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param head ListNode类 the head node * @return ListNode类 */ public ListNode sortInList (ListNode head) { // write code here if (head.next == null) return head; List<int> list = new List<int>(); ListNode t = head; while (t != null) { list.Add(t.val); t = t.next; } list.Sort(); ListNode res = new ListNode(-1); ListNode tail = res; for (int i = 0; i < list.Count; i++) { ListNode temp = new ListNode(list[i]); tail.next = temp; tail = temp; } return res.next; } }