题解 | #排序奇升偶降链表#
排序奇升偶降链表
https://www.nowcoder.com/practice/3a188e9c06ce4844b031713b82784a2a
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类 * @return ListNode类 */ public ListNode sortLinkedList (ListNode head) { // write code here if (head == null) return null; List<int> lsN = new List<int>(); ListNode lnR = head; while (head != null) { int nVal = head.val; int nFindIndex = lsN.FindIndex(r => r > nVal); if (nFindIndex >= 0) lsN.Insert(nFindIndex, nVal); else lsN.Add(nVal); head = head.next; } head = lnR; int nIndex = 0; while (head != null) { head.val = lsN[nIndex]; head = head.next; nIndex++; } return lnR; } }