LEETCODE: Merge Two Sorted Lists
来源:互联网 发布:多益网络是什么 编辑:程序博客网 时间:2024/04/27 19:22
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 == NULL) return l2; if(l2 == NULL) return l1; ListNode *newhead = l1->val < l2->val ? l1 : l2; while(l1 != NULL && l2 != NULL) { if(l1->val < l2->val) { ListNode* last = NULL; while(l1 != NULL && l1->val < l2->val) { last = l1; l1 = l1->next; } last->next = l2; } else { ListNode* last = NULL; while(l2 != NULL && l1->val >= l2->val) { last = l2; l2 = l2->next; } last->next = l1; } } return newhead; }};
0 0
- LeetCode: Merge Two Sorted Lists
- [Leetcode] Merge Two Sorted Lists
- Leetcode: Merge Two Sorted Lists
- [LeetCode] Merge Two Sorted Lists
- [Leetcode] Merge two sorted lists
- leetcode Merge Two Sorted Lists
- [LeetCode]Merge Two Sorted Lists
- [leetcode]Merge Two Sorted Lists
- LeetCode-Merge Two Sorted Lists
- [leetcode] Merge Two Sorted Lists
- [LeetCode] Merge Two Sorted Lists
- LeetCode - Merge Two Sorted Lists
- LeetCode:Merge Two Sorted Lists
- LeetCode | Merge Two Sorted Lists
- 【leetcode】Merge Two Sorted Lists
- Leetcode: Merge Two Sorted Lists
- 【LeetCode】Merge Two Sorted Lists
- Leetcode Merge Two Sorted Lists
- 安卓Button边框的绘制
- 微信支付 - 「App集成」
- iis7 HTTP 错误 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求。解决办法
- 获取音频的专辑图与视频的缩略图
- myeclipse+maven实现多模块项目struts+spring+mybatis,详解
- LEETCODE: Merge Two Sorted Lists
- jquery自定义动画
- ExtJs控件属性详细配置
- Maven by Example Chapter 2. Installing Maven
- XXX is not translated in XXX 【安卓 adt 报错】
- iOS中正确的截屏姿势(代码)
- linux下运维常用命令总结
- Apache与Tomcat的区别
- 赛普拉斯蓝牙BLE---Component API Reference