LeetCode: Merge Two Sorted Lists
2014-06-25 14:39
211 查看
思路:基本的合并两个有序链表,注意边界条件的处理即可。注意题目中的要求,原链表上整合两条链表,所以不要额外开辟一个新节点链表。
code:
code:
class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode *h, *cur; if(l1 == NULL || l2 == NULL)return l1 == NULL ? l2 : l1; if(l1->val < l2->val){ h = cur = l1; l1 = l1->next; } else{ h = cur = l2; l2 = l2->next; } while(l1 && l2){ if(l1->val < l2->val){ cur->next = l1; cur = l1; l1 = l1->next; } else{ cur->next = l2; cur = l2; l2 = l2->next; } } if(l1)cur->next = l1; if(l2)cur->next = l2; return h; } };
相关文章推荐
- LEETCODE-Merge Two Sorted Lists
- LeetCode 21 Merge Two Sorted Lists
- LeetCode Online Judge 题目C# 练习 - Merge Two Sorted Lists
- leetcode 第21题<Merge Two Sorted Lists>(java)
- [Leetcode] Merge two sorted lists 合并两已排序的链表
- LeetCode - Merge Two Sorted Lists
- 【JAVA、C++】LeetCode 021 Merge Two Sorted Lists
- [leetcode]Merge Two Sorted Lists
- leetcode 30: Merge Two Sorted Lists
- LeetCode-21. Merge Two Sorted Lists
- Leetcode: Merge Two Sorted Lists
- LeetCode 21 — Merge Two Sorted Lists(C++ Java Python)
- leetcode 之 Merge Two Sorted Lists
- LeetCode题解: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