Merge Two Sorted Lists
2013-11-10 10:07
274 查看
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) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. if(l1 == NULL) return l2; if(l2 == NULL) return l1; ListNode *head = NULL; if(l1->val <= l2->val){ head = l1; head->next = mergeTwoLists(l1->next, l2); } else { head = l2; head->next = mergeTwoLists(l1, l2->next); } return head; } };
相关文章推荐
- #define用法集锦
- #define用法集锦
- linux字符界面下root用户无法登录成功
- 吐槽下奇葩的公司面试经历
- 吐槽下奇葩的公司面试经历
- 开心菜鸟学习系列学习笔记------------nodejs util公共函数
- Android Service完全解析,关于服务你所需知道的一切(下)
- 自己写的一个简单的Java GUI源码
- linux下访问12306问题
- Android Service完全解析,关于服务你所需知道的一切(上)
- CSS布局-1
- 生成树计数问题
- 开心学习系列学习笔记-----nodejs缺点
- 减少.NET应用程序内存占用经验分享
- Linux Kernel源代码目录
- 关于Tcp三次握手的思考
- gvim & vim
- ubuntu update error
- debian下QT4编程环境的建立
- 通过脚本自动设置,批量实现免密码登录主机