Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
44 lines (36 sloc) 966 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
public class ListNode {
public var val: Int
public var next: ListNode?
public init(_ val: Int) {
self.val = val
self.next = nil
}
}
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
}
}
}