본문 바로가기
Problem Solving/리트코드

[리트코드 leetcode] 147. Insertion Sort List

by ggyongi 2021. 4. 14.
반응형

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
        
 

비전공자 네카라 신입 취업 노하우

시행착오 끝에 얻어낸 취업 노하우가 모두 담긴 전자책!

kmong.com

댓글