MCPcopy
hub / github.com/HuberTRoy/leetCode / sortList

Method sortList

Sorted/SortList.py:119–182  ·  view source on GitHub ↗

:type head: ListNode :rtype: ListNode

(self, head)

Source from the content-addressed store, hash-verified

117
118class Solution(object):
119 def sortList(self, head):
120 """
121 :type head: ListNode
122 :rtype: ListNode
123 """
124 lists = []
125
126 while head:
127 lists.append(head.val)
128 head = head.next
129
130
131 def merge_sort(l, r):
132
133 _l = 0
134 _r = 0
135
136 _l_length = len(l)
137 _r_length = len(r)
138
139 result = []
140
141 while _l < _l_length and _r < _r_length:
142 if l[_l] < r[_r]:
143 result.append(l[_l])
144 _l += 1
145 else:
146 result.append(r[_r])
147 _r += 1
148
149 if _l == _l_length:
150 while _r < _r_length:
151 result.append(r[_r])
152 _r += 1
153 else:
154 while _l < _l_length:
155 result.append(l[_l])
156 _l += 1
157
158 return result
159
160 def split(l):
161 if len(l) <= 1:
162 return l
163
164 _l = split(l[:len(l)//2])
165 _r = split(l[len(l)//2:])
166
167 return merge_sort(_l, _r)
168
169 lists = split(lists)
170
171 if not lists:
172 return None
173
174 head = ListNode(lists[0])
175
176 _head = head

Callers

nothing calls this directly

Calls 2

splitFunction · 0.85
ListNodeClass · 0.85

Tested by

no test coverage detected