Merge Two Sorted Lists
2016-03-14 10:48
302 查看
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.struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *helper=new ListNode(0);
ListNode *head=helper;
while(l1 && l2){
if(l1->val< l2->val){
helper->next=l1;
l1=l1->next;
}
else{
helper->next=l2;
l2=l2->next;
}
helper=helper->next;
}
if(l1)
helper->next=l1;
if(l2)
helper->next=l2;
return head->next;
}
two lists.struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *helper=new ListNode(0);
ListNode *head=helper;
while(l1 && l2){
if(l1->val< l2->val){
helper->next=l1;
l1=l1->next;
}
else{
helper->next=l2;
l2=l2->next;
}
helper=helper->next;
}
if(l1)
helper->next=l1;
if(l2)
helper->next=l2;
return head->next;
}
相关文章推荐
- Druid连接池简单入门配置
- MediaPlayer
- 应用程序权限设计
- 10款流行的Markdown编辑器
- iOS程序内发短信
- MongoDB 3 + Windows 7 X64安装及配置
- MyBatis——基础
- 【poj1821】Fence 单调队列优化DP
- Session和Cookie的关系
- Problem E
- 第五天git学习记录
- Android 最火的快速开发框架XUtils
- php 数组 添加元素、删除元素
- 4296: [PA2015]Mistrzostwa|图的联通
- cmd中乱码暂时解决
- this bind apply call
- java中封装的概念
- iOS强制切换横屏、竖屏
- Android IPC机制之ContentProvider
- C++之奇技淫巧