My Swift Solution


  • 0
    J
    func mergeTwoLists(l1: ListNode?, _ l2: ListNode?) -> ListNode? {
            var smallerNode: ListNode? = nil
    
            if l1 == nil {
                return l2
            } else if l2 == nil {
                return l1
            } else {
                if l1!.val <= l2!.val {
                    smallerNode = l1
                    smallerNode?.next = self.mergeTwoLists(l1?.next, l2)
                } else {
                    smallerNode = l2
                    smallerNode?.next = self.mergeTwoLists(l1, l2?.next)
                }
            }
    
            return smallerNode
        }

  • 0
    J

    This is not a really in-place method


Log in to reply
 

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