Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
32 lines (28 sloc) 783 Bytes
//
// MergeTwoSortedLists.swift
// LeetCode.swift
//
// Created by 叶帆 on 2017/11/11.
// Copyright © 2017年 Suzhou Coryphaei Information&Technology Co., Ltd. All rights reserved.
//
/**
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
*/
import Foundation
class Solution {
func mergeTwoLists(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
if l1 == nil {
return l2
}
if l2 == nil {
return l1
}
if l1!.val < l2!.val {
l1?.next = mergeTwoLists(l1?.next, l2)
return l1
} else {
l2?.next = mergeTwoLists(l1, l2?.next)
return l2
}
}
}