leetcode.com/problems/insertion-sort-list/
Insertion Sort List - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
풀긴 하였지만 소요 시간이 매우 길다..
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
root = node = ListNode(None)
while head:
node = root
while node.next and node.next.val < head.val:
node = node.next
node.next = ListNode(head.val,node.next)
head = head.next
return root.next
댓글