# Leetcode 21 Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

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

}
p->next=l1?l1:l2;
return result->next;
}
};

## LeetCode 21 Merge Two Sorted Lists (C,C++,Java,Python)

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

## LeetCode算法题——21. Merge Two Sorted Lists

• LZTree
• 2017年04月12日 17:01
• 149

## leetcode[21 链表]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 21. Merge Two Sorted Lists

1.题目 Merge two sorted linked lists and return it as a new list. The new...

## LeetCode_21---Merge Two Sorted Lists

LeetCode

举报原因： 您举报文章：Leetcode 21 Merge Two Sorted Lists 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)