Sorting linked list with mutation (oh no)

This commit is contained in:
2023-10-21 08:41:41 -04:00
parent 337a795ad0
commit a8d82b180a
2 changed files with 57 additions and 5 deletions
+7 -5
View File
@@ -187,13 +187,15 @@ def sort_linked_list(head: stdlib.ListNode | None) -> stdlib.ListNode | None:
cur = ret
for _, node in sorted(by_val):
for _, node in sorted(by_val, key=lambda v: v[0]):
if cur is None:
cur = ret = stdlib.ListNode(node.val)
cur = ret = node
continue
next_node = stdlib.ListNode(node.val)
cur.next = next_node
cur = next_node
cur.next = node
cur = cur.next
if cur is not None:
cur.next = None
return ret