-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path148.SortList.cs
40 lines (39 loc) · 952 Bytes
/
148.SortList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// 148. Sort List
// Given the head of a linked list, return the list after sorting it in ascending order.
// Example 1:
// Input: head = [4,2,1,3]
// Output: [1,2,3,4]
// Example 2:
// Input: head = [-1,5,3,4,0]
// Output: [-1,0,3,4,5]
// Example 3:
// Input: head = []
// Output: []
/**
* 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) {
List<int> list = new List<int>();
ListNode temp = head;
while(temp!=null){
list.Add(temp.val);
temp = temp.next;
}
list.Sort();
temp = head;
foreach(var item in list){
temp.val = item;
temp = temp.next;
}
return head;
}
}