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

java

```java
import leetcode.common.LinkedUtils;
import leetcode.common.ListNode;

/**
 * 合并两个排序的链表
 *
 * 输入两个单调递增的链表，输出两个链表合成后的链表，
 * 当然我们需要合成后的链表满足单调不减规则。
 */
public class MergeSolution {
    public ListNode Merge(ListNode list1, ListNode list2) {
        ListNode list3=new ListNode(0);
        ListNode tmp=list3;

        while (list1!=null&&list2!=null){
            if(list2.val>list1.val){
                tmp.next=list1;
                tmp=tmp.next;
                list1=list1.next;
            }else {
                tmp.next=list2;
                tmp=tmp.next;
                list2=list2.next;
            }
        }

        if(list1==null){
            tmp.next=list2;
        }
        if(list2==null){
            tmp.next=list1;
        }
        return list3.next;
    }

    public static void main(String[] args){
        int[] arr1={
                4,8,9
        };
        int[] arr2={
                1,6,7
        };
        ListNode list1=LinkedUtils.arrayToLinkedList(arr1);
        ListNode list2=LinkedUtils.arrayToLinkedList(arr2);

        ListNode r=new MergeSolution().Merge(list1,list2);
        LinkedUtils.print(r);
    }
}
```


---

# 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://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/shu-ju-jie-gou-lei/73-he-bin-liang-ge-pai-xu-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.
