[leetcode Merge Two Sorted Lists]week 15
2017-07-07 02:02
316 查看
一、题目
Merge two sorted linked lists and return itas a new list. The new list should be made by splicing together the nodes ofthe first two lists.
二、代码
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* head=new ListNode(0);
ListNode*p=head;
while(l1!=NULL || l2!=NULL)
{
if(l1==NULL)
{
p->next=l2;
l2=l2->next;
}
elseif (l2==NULL)
{
p->next=l1;
l1=l1->next;
}
else
{
if(l1->val < l2->val)
{
p->next=l1;
l1=l1->next;
}
else
{
p->next=l2;
l2=l2->next;
}
}
p=p->next;
}
returnhead->next;
}
};
三、思路
两个链表两个指针的val比对将新链表指针指向较小的节点。
Merge two sorted linked lists and return itas a new list. The new list should be made by splicing together the nodes ofthe first two lists.
二、代码
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* head=new ListNode(0);
ListNode*p=head;
while(l1!=NULL || l2!=NULL)
{
if(l1==NULL)
{
p->next=l2;
l2=l2->next;
}
elseif (l2==NULL)
{
p->next=l1;
l1=l1->next;
}
else
{
if(l1->val < l2->val)
{
p->next=l1;
l1=l1->next;
}
else
{
p->next=l2;
l2=l2->next;
}
}
p=p->next;
}
returnhead->next;
}
};
三、思路
两个链表两个指针的val比对将新链表指针指向较小的节点。
相关文章推荐
- leetcode 21:Merge Two Sorted Lists(15-10-9)
- 算法分析与设计课程——LeetCode刷题之Merge Two Sorted Lists
- leetcode 30: Merge Two Sorted Lists
- LeetCode:Merge Two Sorted Lists
- Leetcode Merge Two Sorted Lists(合并两个有序表)
- Leetcode 21 Merge Two Sorted Lists
- LeetCode 21:Merge Two Sorted Lists
- [Leetcode] 21. Merge Two Sorted Lists
- LeetCode | Merge Two Sorted Lists
- leetcode Merge Two Sorted Lists
- LeetCode-Merge Two Sorted Lists
- LeetCode - Merge Two Sorted Lists
- 【Leetcode】之Merge Two Sorted Lists
- [LeetCode] Merge Two Sorted Lists
- LeetCode_Merge Two Sorted Lists
- LeetCode解题报告--Merge Two Sorted Lists
- LeetCode 021 Merge Two Sorted Lists
- 【leetcode】Merge Two Sorted Lists(easy)
- 【leetcode】Merge Two Sorted Lists
- leetcode第一刷_Merge Two Sorted Lists