Swift.合并两个排序的链表

cubegao 2015-07-09 PM 365℃ 0条
题目描述:
import Foundation

class For17Solution {
    func mergeListNode(_ head1: ListNode?,_ head2: ListNode?) -> ListNode? {
        
        if head1 == nil {
            return head2
        }
        
        if head2 == nil {
            return head1
        }
        
        var newHead : ListNode? = nil
        var pHead = head1
        var qHead = head2
        
        if pHead!.val < qHead!.val {
            newHead = pHead
            pHead = pHead?.next
        }else {
            newHead = qHead
            qHead = qHead?.next
        }
        
        var tempHead = newHead
        while pHead != nil && qHead != nil {
            
            if pHead!.val < qHead!.val {
                tempHead?.next = pHead
                pHead = pHead?.next
            }else {
                tempHead?.next = qHead
                qHead = qHead?.next
            }
            tempHead = tempHead?.next
        }
        
        if pHead != nil {
            tempHead?.next = pHead
        }
        
        if qHead != nil {
            tempHead?.next = qHead
        }
        
        return newHead
        
    }
    
    func mergeListNode2(_ head1: ListNode?,_ head2: ListNode?) -> ListNode? {

        if head1 == nil {
            return head2
        }
        
        if head2 == nil {
            return head1
        }
        
        var newHead: ListNode? = nil
        
        if head1!.val < head2!.val {
            newHead = head1
            newHead?.next = mergeListNode2(head1?.next, head2)
        }else {
            newHead = head2
            newHead?.next = mergeListNode2(head1, head2?.next)
        }
        
        return newHead
    }
}

算法思想:有点类似合并两个排序数组。递归的话,相对好理解一点。

github地址:https://github.com/cubegao/LeetCode

标签: 算法

非特殊说明,本博所有文章均为博主原创。

评论啦~