【21】Merge Two Sorted Lists
2016-08-15 15:50
113 查看
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.
简单的链表题,加一个头节点方便编码
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* head=new ListNode(0);
ListNode* p=head;
while(l1 || l2){
if((l1 && l2 && (l1->val < l2->val)) || !l2){
p->next=l1;
l1=l1->next;
p=p->next;
}
else{
p->next=l2;
l2=l2->next;
p=p->next;
}
}
return head->next;
}
简单的链表题,加一个头节点方便编码
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* head=new ListNode(0);
ListNode* p=head;
while(l1 || l2){
if((l1 && l2 && (l1->val < l2->val)) || !l2){
p->next=l1;
l1=l1->next;
p=p->next;
}
else{
p->next=l2;
l2=l2->next;
p=p->next;
}
}
return head->next;
}
相关文章推荐
- LeetCode 21 — Merge Two Sorted Lists(C++ Java Python)
- 21. Merge Two Sorted Lists
- 21 - Merge Two Sorted Lists - LeetCode
- LeetCode_OJ【21】Merge Two Sorted Lists
- LeetCode-21. Merge Two Sorted Lists
- 21 Merge Two Sorted Lists
- [LeetCode] 21. Merge Two Sorted Lists
- [LeetCode]21. Merge Two Sorted Lists
- LeetCode-21 Merge Two Sorted Lists(合并两个有序链表)
- LeetCode 21 Merge Two Sorted Lists (C,C++,Java,Python)
- Leetcode 21. Merge Two Sorted Lists(C++)
- LeetCode-21-Merge Two Sorted Lists(C语言实现)
- Leetcode 21. Merge Two Sorted Lists
- Lettcode_21_Merge Two Sorted Lists
- leetcode 21. Merge Two Sorted Lists
- LeetCode 21 Merge Two Sorted Lists
- [leetcode]21. Merge Two Sorted Lists
- 编程题2- Merge Two Sorted Lists(leetcode-21)
- Leetcode 21 Merge Two Sorted Lists 将两个已排序的链表合并起来
- Leetcode 21. Merge Two Sorted Lists