From c779dcecf88452292940c51dc99e92841ea06555 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Wed, 10 Jul 2024 15:25:21 +0800 Subject: [PATCH] 20240710 Finished --- LeetCodeSharp/Problems/Solution148.cs | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 LeetCodeSharp/Problems/Solution148.cs diff --git a/LeetCodeSharp/Problems/Solution148.cs b/LeetCodeSharp/Problems/Solution148.cs new file mode 100644 index 0000000..e0297ce --- /dev/null +++ b/LeetCodeSharp/Problems/Solution148.cs @@ -0,0 +1,51 @@ +// [148] Sort List + +using System.Collections.Generic; +using LeetCodeSharp.Utils; + +namespace LeetCodeSharp.Problems148 +{ + + // Submission codes start here + + /** + * Definition for singly-linked list. + * public class ListNode { + * public int val; + * public ListNode next; + * public ListNode(int val=0, ListNode next=null) { + * this.val = val; + * this.next = next; + * } + * } + */ +public class Solution { + public ListNode SortList(ListNode head) + { + var array = new List(); + + while (head != null) + { + array.Add(head.val); + + head = head.next; + } + + array.Sort(); + + var dummyHead = new ListNode(); + var node = dummyHead; + + foreach (var i in array) + { + var newNode = new ListNode(i); + node.next = newNode; + node = newNode; + } + + return dummyHead.next; + } +} + + // Submission codes end here +} \ No newline at end of file