Merge Two Sorted Lists
2015-06-14 20:17
176 查看
Description:
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.
Code:
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.
Code:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1) return l2; else if (!l2) return l1; else { ListNode * head, *p, *q, *result; if (l1->val < l2->val) { head = l1; p = l1->next; q = l2; } else { head = l2; p = l1; q = l2->next; } result = head; while (p&&q) { if (p->val < q->val) { result->next = p; result = p; p = p->next; } else { result->next = q; result = q; q = q->next; } } result->next = (p==NULL)?q:p; return head; } }
相关文章推荐
- bootstrap布局:嵌套布局和流动布局
- 第四节:设计支持加载项的应用程序
- 【并发编程实战笔记】--使用锁和条件变量的线程安全队列
- QT相对路径显示图片
- java P2P JXTA-JXSE学习(一)
- 大道至简前段
- Latex一些问题整理
- c++ multimap multiset
- 截图与屏幕录像利器:FastStone Capture
- win 10待机唤醒后无法连接wifi怎办?
- Yii 中使用 yii-bootstrap 扩展
- Codeforces 551C - GukiZ hates Boxes(二分加贪心)
- Android开发之XML文件的解析的三种方法
- Codeforces 2A :winner
- 分块大法好
- Codeforces 2A :winner
- android 文件下载
- spark基础(三)------------------------使用maven构建一个基于scala的spark应用程序。
- 最新php环境搭建
- 保存现场数据和状态:onSaveInstanceState\onRestoreInstanceState\onCreate()