public static ListNode <T> Sort <T>(ListNode <T> head) { ListNode <T>[] sorted = LinkedListUtilities.Enumerate(head).OrderBy(o => o.Value).ToArray(); ListNode <T> sentinel = new ListNode <T>(); ListNode <T> tail = sentinel; foreach (ListNode <T> current in sorted) { tail.Next = current; current.Previous = tail; tail = current; } sentinel.Next.Previous = null; return(sentinel.Next); }
public static T[] ToArray <T>(ListNode <T> head) { return(LinkedListUtilities.Enumerate(head).Select(o => o.Value).ToArray()); }
public static void Append <T>(ListNode <T> a, ListNode <T> b) { ListNode <T> tail = LinkedListUtilities.Tail(a); tail.Next = b; }