LintCode - Merge Two Sorted List
来源:互联网 发布:阿里云怎么绑定支付宝 编辑:程序博客网 时间:2024/05/21 17:46
LintCode - Merge Two Sorted Lists
- LintCode - Merge Two Sorted Lists
- Web Link
- Description
- Code - C
- Tips
Web Link
http://www.lintcode.com/en/problem/merge-two-sorted-lists/
Description
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order.
Example
Given 1->3->8->11->15->null, 2->null , return 1->2->3->8->11->15->null.
Code - C++
/** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } */class Solution {public: /** * @param ListNode l1 is the head of the linked list * @param ListNode l2 is the head of the linked list * @return: ListNode head of linked list */ ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { // write your code here if (l1 == NULL) { return l2; } if (l2 == NULL) { return l1; } ListNode* head = l1->val < l2->val ? l1:l2; ListNode* temp = new ListNode(0); while (l1 != NULL && l2 != NULL) { if (l1->val < l2->val) { temp->next = l1; temp = l1; l1 = l1->next; } else { temp->next = l2; temp = l2; l2 = l2->next; } } if (l1 == NULL) { temp->next = l2; } if (l2 == NULL) { temp->next = l1; } return head; }};
Tips
None
0 0
- LintCode - Merge Two Sorted List
- Merge Two Sorted List
- merge two sorted list
- Merge Two Sorted List
- Merge Two Sorted List
- Merge Two sorted list
- [Lintcode]Merge Two Sorted Lists
- lintcode:Merge Two Sorted Lists
- leetcode merge two sorted list
- Merge Two Sorted List ---LeetCode
- 【leetcode】 merge two sorted list
- Merge two sorted list [leetcode]
- Merge two sorted linked list
- Merge Two Sorted Single-linke List
- merge two sorted linked list with duplicates
- merge two sorted linked list without duplicates
- merge two sorted linked list with duplicates
- LeetCode 64. Merge Two Sorted List
- 暑假规划及小学期总结
- 有效解决win8/8.1系统安装.net framework 3.5出现0x800F0906代码错误
- 关于类静态成员变量指针通过动态分配的内存如何回收的探讨
- Android事件分发机制完全解析,带你从源码的角度彻底理解(下)
- 我的jenkins自动部署方案演进史(插件和自定义脚本)
- LintCode - Merge Two Sorted List
- hdoj 1869 六度分离 【判断任意点最短路是否小于等于7】
- 小贝_mysql优化学习
- Linux下进程的内存结构
- 字典树
- 19-24 周记 总结
- 一起talk C栗子吧(第二十二回:C语言实例--队列一)
- c++中写udp编程
- 虚函数与虚继承寻踪