[LeetCode]Merge Two Sorted Lists
2015-11-05 23:22
330 查看
题目描述:(链接)
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.
解题思路:
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.
解题思路:
/** * 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 != nullptr && l2 != nullptr) { if (l1->val < l2->val) { p->next = l1; l1 = l1->next; } else { p->next = l2; l2 = l2->next; } p = p->next; } if (!l1) { p->next = l2; } else { p->next = l1; } return dummy.next; } };
相关文章推荐
- python线程详解
- CSS3实现Bootstrap的tooltip动画提示效果
- java设计模式--原型模式(Prototype)
- python中对list去重的多种方法
- EasyMock使用方法和原理详解
- startService和bindService混合使用分析
- json学习笔记_01_json语法基本格式并s会用java读取json
- i2c--2.6.34文档:如何枚举产生i2c_client
- j2ee web.xml 中url-pattern匹配
- Caused by: java.io.EOFException: Can not read response from server.
- mysql 学习记录(二十三)--mysql安装时相关优化
- opencv3 寻找亚像素角点-cornerSubPix函数-滚动条
- Linux下创建桌面快捷方式
- 【笔记】程序员的思维修炼4
- spring hadoop系列一
- Ubuntu环境下sublime3 nodejs安装与插件配置
- NSPredicate 谓词
- java进阶-创建和销毁对象
- 初学java的一些小结
- opencv3 寻找亚像素角点-cornerSubPix函数