LeetCode(21)题解:Merge Two Sorted Lists
2015-06-18 23:16
330 查看
https://leetcode.com/problems/merge-two-sorted-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.
思路:
考察链表操作,没啥说的。
AC代码:
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.
思路:
考察链表操作,没啥说的。
AC代码:
/** * 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) { ListNode *p=new ListNode(0); ListNode *q=p; while(l1!=NULL && l2!=NULL){ if(l1->val<l2->val){ q->next=l1; q=q->next; l1=l1->next; } else{ q->next=l2; q=q->next; l2=l2->next; } } if(l1!=NULL) q->next=l1; else q->next=l2; p=p->next; return p; } };
相关文章推荐
- JAVA 异常对于性能的影响
- 个人读后感
- JAVA 异常对于性能的影响
- Hibernate实体关系映射——双边的多对一、一对多关系
- layout_gravity和gravity的区别
- ssh免密码登录
- 对其他组的评价与反馈
- 希捷个人云:家用NAS的解决方案
- 程序员面试什么最重要?
- 本地Git仓库同步到Bitbucket 远程Git仓库
- inet_ntoa()使用注意事项
- HDU 1213
- 2015061804 - 10年程序开发经验总结(1)
- cocos2dx3.0rc导出自定义类到lua的方法详细步骤
- log4j xml配置详解
- C++ explicit<转>
- miniSTM32开发板的一键下载电路
- Pow(x, n)
- 《管理概论》第三周 课堂笔记与学习心得
- JavaScript javaTomcat