# 21. Merge Two Sorted Lists

{% hint style="info" %}
注意dummy node的用法
{% endhint %}

{% tabs %}
{% tab title="Python" %}

```python
class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        # edge case
        if l1 == None and l2 != None:
            return l2
        if l1 != None and l2 == None:
            return l1
        if l1 == None and l2 == None:
            return l1
        
        dummy = ListNode(0)
        head = dummy
            
        while l1 != None and l2 != None:
            if l1.val <= l2.val:
                head.next = l1
                l1 = l1.next
            else:
                head.next = l2
                l2 = l2.next
            head = head.next
            
        if l1 != None:
            head.next = l1
        elif l2 != None:
            head.next = l2
            
        return dummy.next
```

{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}


---

# 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://r24zeng.gitbook.io/leetcode-notebook/binary-search-and-tree/21.-merge-two-sorted-lists.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.
