Leetcode43: Merge Two Sorted Lists
2015-07-02 17:25
441 查看
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.
用归并排序的思想。
two lists.
用归并排序的思想。
/** * 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) { ListNode* merge = new ListNode(0); ListNode* head = merge; while(l1&&l2) { if(l1->val < l2->val) { merge->next = l1; l1 = l1->next; merge = merge->next; } else { merge->next = l2; l2 = l2->next; merge = merge->next; } } if(l1) { merge->next = l1; } if(l2) { merge->next = l2; } return head->next; } };
相关文章推荐
- 还是并发
- 1. 网页组成
- 插入节点insertBefore()
- 【Web探索之旅】第二部分第二课:服务器语言
- Linux puppet的安装配置部署
- iOS常见崩溃情况以及bug调试解决方法
- muduo启程
- OJ第三批——Problem U:编程题:运算符重载---矩阵求和
- opencv2.4.9在vs2013中丢失opencv_core249d.dll问题及解决
- iOS中使用block块的用法
- 安装freeswitch碰到的问题
- cocos js 发布
- unity3d环境安装指南: Unity 4.5.5 + Visual Studio 2010
- 每秒如何接收上百万数据包
- 如何使用渲云客户端进行渲染?
- NSArray.h翻译
- Spring MVC 教程,快速入门,深入分析
- 使用adns库解析域名
- Android:报" Incorrect line ending: found carriage return (\r) without corresponding newline (\n) "解决
- 20150702-CitrixXenDesktop-SQL Server