Merge Two Sorted Lists 分类: Leetcode(排序) 2015-04-08 21:59 24人阅读 评论(0) 收藏
2015-04-08 21:59
197 查看
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 twolists.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
if(!l1) return l2;
if(!l2) return l1;
ListNode dummy(-1);
ListNode *p = &dummy;
while(l1 &&l2) {
if(l1->val >= l2->val) {
p->next = l2;
p = l2;
l2 = l2->next;
}
else {
p->next = l1;
p = l1;
l1 = l1->next;
}
}
if(l1) p->next = l1;
else p->next = l2;
return dummy.next;
}
};
相关文章推荐
- Merge Sorted Array 分类: Leetcode(排序) 2015-04-08 21:52 24人阅读 评论(0) 收藏
- Search a 2D Matrix 分类: Leetcode(查找) 2015-04-11 10:19 24人阅读 评论(0) 收藏
- Palindrome Partitioning 分类: Leetcode(动态规划) 2015-04-14 11:00 24人阅读 评论(0) 收藏
- Insertion Sort List 分类: Leetcode(排序) 2015-04-09 11:26 23人阅读 评论(0) 收藏
- leetCode(24):Binary Search Tree Iterator 分类: leetCode 2015-06-23 11:50 192人阅读 评论(0) 收藏
- First Missing Positive 分类: Leetcode(排序) 2015-04-09 17:13 25人阅读 评论(0) 收藏
- Search for a Range 分类: Leetcode(查找) Leetcode(排序) 2015-04-10 15:34 23人阅读 评论(0) 收藏
- 数字排序,波动增长 分类: python 小练习 2013-07-04 19:01 281人阅读 评论(0) 收藏
- Excel Sheet Column Title 分类: Leetcode 2015-01-15 11:08 87人阅读 评论(0) 收藏
- Remove Element 分类: Leetcode(线性表) 2015-01-29 10:47 55人阅读 评论(0) 收藏
- N-Queens II 分类: Leetcode 2014-12-24 14:17 85人阅读 评论(0) 收藏
- Dungeon Game 分类: Leetcode 2015-01-15 09:10 69人阅读 评论(0) 收藏
- Unique Binary Search Trees 分类: Leetcode 2014-12-07 11:30 81人阅读 评论(0) 收藏
- SQL ID自增列从1开始重新排序 分类: SQL Server 2014-05-19 14:46 652人阅读 评论(0) 收藏
- 冒泡排序... 分类: java 2009-11-02 14:09 224人阅读 评论(0) 收藏
- Median of Two Sorted Arrays 分类: Leetcode(线性表) 2015-02-04 09:05 54人阅读 评论(0) 收藏
- Find Minimum in Rotated Sorted Array II 分类: Leetcode 2015-01-03 10:57 78人阅读 评论(0) 收藏
- php 二维数组排序比较 1 —— 冒泡排序法 分类: php 冒泡排序 二维数组排序 2014-06-24 11:28 188人阅读 评论(0) 收藏
- php 二维数组排序比较 2 —— 快速排序法 分类: php 二维数组排序 快速排序 2014-06-24 11:39 190人阅读 评论(0) 收藏
- 快速排序 分类: 算法 2014-10-10 11:05 541人阅读 评论(0) 收藏