# Leetcode: Merge Two Sorted Lists

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *ans=new ListNode(0);
while(l1 && l2){
if(l1->val<l2->val){
ans->next=l1;
ans=ans->next;
l1=l1->next;
}
else{
ans->next=l2;
l2=l2->next;
ans=ans->next;
}
}
if(l1){
ans->next=l1;
}
if(l2){
ans->next=l2;
}

}
};

• 本文已收录于以下专栏：

## [LeetCode] Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t...

## LeetCode | Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t...

## LeetCode|Merge Two Sorted Lists

【问题描述】 Merge two sorted linked lists and return it as a new list. The new list should be made by sp...

## LeetCode - Merge Two Sorted Lists

Merge Two Sorted Lists LeetCode 合并两个有序链表题目

## leetcode Merge Two Sorted Lists

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)