• 파이썬 TeamSort 내장 함수(sort())뼟 이용하여 풂
    • 자바에서도 Collections뼟 이용하면 Teamsort 사용 가능


Solution

# Definition for singly-linked list.
from typing import Optional


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        arr = []
        linked_to = head
        while linked_to:
            arr.append(linked_to.val)
            linked_to = linked_to.next

        arr.sort()

        linked_from = head
        for x in arr:
            linked_from.val = x
            linked_from = linked_from.next

        return head


Reference

Leave a comment