Swift solution got TLE


  • 0
    F

    I can't identify what the problem is. Can anyone please help me with this issue?

    class Solution {
         func insertionSortList(_ head: ListNode?) -> ListNode? {
            if head == nil || head?.next == nil {
               return head
            }
            let dummy = ListNode(Int.min)
            var prev  = dummy
            var cur = head
            while cur != nil {
               let next = cur?.next
               prev = dummy
               while prev.next != nil && prev.next!.val < cur!.val {
                    prev = prev.next!
               }
               cur?.next = prev.next
               prev.next = cur
               cur = next
           }
          return dummy.next
    }

  • 0

    @fiona8957 Fixed.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.