[leetcode 24] Merge Two Sorted Lists
2014-08-06 15:26
399 查看
题目:
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 firsttwo lists.
思路:
1.同时遍历,一一比较即可代码:
class Solution { public: ListNode *mergeTwoLists(ListNode *l1,ListNode *l2) { ListNode *head=new ListNode(-1); ListNode *tail=head,*p1=l1,*p2=l2; while(p1 && p2) { if(p1->val<=p2->val) { tail->next=p1; p1=p1->next; } else { tail->next=p2; p2=p2->next; } tail=tail->next; } tail->next=!p1?p2:p1; return head->next; } };
相关文章推荐
- leetcode(21). Merge Two Sorted Lists
- leetcode - Merge Two Sorted Lists
- LeetCode:Merge Two Sorted Lists
- leetcode 21. Merge Two Sorted Lists
- [leetcode]Merge Two Sorted Lists
- 【leetcode c++】21 Merge Two Sorted Lists
- LeetCode OJ 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 21 - Merge Two Sorted Lists
- LeetCode21 Merge Two Sorted Lists
- LeetCode_Merge Two Sorted Lists
- leetcode——21——Merge Two Sorted Lists
- [LeetCode] Merge Two Sorted Lists
- 【leetcode每日一题】NO21.Merge Two Sorted Lists
- LeetCode Merge Two Sorted Lists
- 【LeetCode】Merge Two Sorted Lists