# 25.合并两个排序的链表

## 题目描述

输入两个递增排序的链表，合并这两个链表并使新链表中的节点仍然是递增排序的。

示例1：

输入：1->2->4, 1->3->4 输出：1->1->2->3->4->4 限制：

0 <= 链表长度 <= 1000

注意：本题与主站 21 题相同：<https://leetcode-cn.com/problems/merge-two-sorted-lists/>

来源：力扣（LeetCode）

链接：<https://leetcode-cn.com/problems/he-bing-liang-ge-pai-xu-de-lian-biao-lcof> 著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。

## 题解

```swift
func mergeTwoLists(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
    var line1 = l1
    var line2 = l2
    var firstNode: ListNode?
    var lastNode: ListNode?

    while let temp1 = line1, let temp2 = line2 {
        let isL1VSmaller = temp1.val < temp2.val
        let node = ListNode(isL1VSmaller ? temp1.val : temp2.val)
        if firstNode == nil {
            firstNode = node
        }
        else {
            lastNode?.next = node
        }
        lastNode = node
        isL1VSmaller ? (line1 = temp1.next) : (line2 = temp2.next)
    }

    if line1 == nil {
        lastNode?.next = line2
    }
    else {
        lastNode?.next = line1
    }

    return firstNode != nil ? firstNode : (l1 != nil ? l1 : l2)
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ryukiedev.gitbook.io/wiki/shu-ju-jie-gou-yu-suan-fa/jian-zhi-offerswift/25.-he-bing-liang-ge-pai-xu-de-lian-biao.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
