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.

问题:将两个已排序的列表,合并为一个有序列表。

令 head 为两个列表表头中较小的一个,令 p 为新的已排序的最后一个元素。令 l1, l2 分别为两个列表中未排序部分的首节点。依次将 l1, l2 中的较小值追加到 p 后面,并调整 p 和 l1、l2较小者指针即可。

     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {

         if (l1 == NULL) {
return l2;
} if (l2 == NULL) {
return l1;
} ListNode* head = new ListNode();
if (l1->val <= l2->val) {
head = l1;
l1 = l1->next;
}else{
head = l2;
l2 = l2->next;
} ListNode* p = head; while(l1 != NULL && l2 != NULL){ if(l1->val <= l2->val){
p->next = l1;
p = p->next;
l1 = l1->next;
}else{
p->next = l2;
p = p->next;
l2 = l2->next;
}
} if (l1 == NULL) {
p->next = l2;
} if (l2 == NULL) {
p->next = l1;
} return head;
}
 

[LeetCode] 21. Merge Two Sorted Lists 解题思路的更多相关文章

  1. [LeetCode] 21. Merge Two Sorted Lists 合并有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  2. [LeetCode] 21. Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  3. Java [leetcode 21]Merge Two Sorted Lists

    题目描述: Merge two sorted linked lists and return it as a new list. The new list should be made by spli ...

  4. [leetcode] 21. Merge Two Sorted Lists (Easy)

    合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...

  5. [leetcode]21. Merge Two Sorted Lists合并两个链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  6. leetcode 21.Merge Two Sorted Lists ,java

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  7. LeetCode 21. Merge Two Sorted Lists (合并两个有序链表)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  8. LeetCode 21 -- Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  9. Leetcode 21. Merge Two Sorted Lists(easy)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

随机推荐

  1. LSPCI具体解释分析

    一.PCI简单介绍     PCI是一种外设总线规范.我们先来看一下什么是总线:总线是一种传输信号的路径或信道.典型情况是,总线是连接于一个或多个导体的电气连线,总 线上连接的全部设备可在同一时间收到 ...

  2. Robotium -- 针对apk包的测试

    在使用Robotium测试的时候,有时候,测试人员并没有代码权限,而Robotium也可以在只有apk文件进行测试,下面就介绍一下这个过程. 1.设置环境变量 安装jdk环境和sdk环境 2.安装签名 ...

  3. JMeter在里面Json数据处理方法

    http://eclipsesource.com/blogs/2014/06/12/parsing-json-responses-with-jmeter/ Json作为一种数据交换格式在网络开发.特别 ...

  4. 【前端JS】input textarea 默认文字,点击消失

    如题.前端页面的 input textarea 有时候须要显示默认文字以提示用户,下面为实现代码,以 input 为例.textarea 能够直接搬用 HTML <input type=&quo ...

  5. RollPagerView的用法:

    RollPagerView的用法: /** * * @author smiling * @date 2016/10 */ Android Studio 导包: compile 'com.jude:ro ...

  6. Activity 状态的保存和恢复

    Activity状态保存的两种情况 一.Activity状态保持概念 保存Activity的状态是非常重要的,例如我们在玩一个游戏的时候,突然来了一个电话,这个时候在接听完电话之后我们返回到游戏中,这 ...

  7. MyEclipse修改servlet模版

    找到myeclipse安装目录中的 然后把这个jar包复制到桌面 以压缩包的方式打开 之后保存, 然后把修改的这个jar包放到刚开的路径,替换已经存在的! 完成!

  8. ImageIO.wtrie生成jpg图片质量损失方案:BufferedImage生成jpg图片文件流

    Iterator<ImageWriter> iterator = ImageIO.getImageWritersByFormatName("jpeg"); ImageW ...

  9. sql server 导出

    http://ssat.codeplex.com/SourceControl/latest 用于连接sql server

  10. SVN库迁移整理方法总结

    有时候需要从一台机器迁移svn存储库到另外一台机器,如果数据量非常大的话,没有好的方法是很不方便的,其实迁移svn跟迁移mysql差不多,也有导出导入的方案 以下是subversion官方推荐的备份方 ...