-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
148.py
51 lines (43 loc) · 1.37 KB
/
148.py
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
41
42
43
44
45
46
47
48
49
50
51
__________________________________________________________________________________________________
sample 76 ms submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def sortList(self, head: ListNode) -> ListNode:
node = head
ls = []
while node:
ls.append(node.val)
node = node.next
ls.sort()
root = node = head
for val in ls:
node.val = val
node = node.next
return root
__________________________________________________________________________________________________
sample 20400 kb submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def sortList(self, head: ListNode) -> ListNode:
#use the list to solve the problem
list=[]
res=head
while head:
list.append(head.val)
head=head.next
list.sort()
head=res
#Following is important
for part in list:
res.val=part
res=res.next
return head
__________________________________________________________________________________________________