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. win7-64bit 下oracle11g plsql 的正确安装

    本人在PC机上安装了Oracle 11g 版本号的数据库服务,通过PL/SQL连接数据库时总是无法连接,位的oracle客户端工具instantclient进行转换,另外一种plsql的不能安装到文件 ...

  2. c++ map与 qt QMap insert 区别

    当插入相同key的字段时, c++  map 会保留原来的字段, QMap 则会取代原来的字段.

  3. Linux应用程序打包

      原文地址:http://blog.solrex.cn/articles/packaging-1-src.html1. 应用程序打包技术之一(源代码篇) 相信很多朋友都曾经为方便做某件事写过自己的小 ...

  4. 通知中心 NSNotificationCenter

    NSNotificationCenter 通知中心提供了一种在程序内广播信息的途径,一个NSNotificationCenter对象本质上是一个通知分发表(notification dispatch ...

  5. java byte数组与int,long,short,byte转换

    public class DataTypeChangeHelper { /** * 将一个单字节的byte转换成32位的int * * @param b * byte * @return conver ...

  6. 快速优化yum (for centos5.5)

    定义yum超时时间:vi /etc/yum.conftimeout=120 修改源:(全部复制粘贴即可)cd /etc/yum.repos.d/mv rhel-debuginfo.repo rhel- ...

  7. Python基础类型

    1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 names = ['Alex',"Tenglan",'Eric ...

  8. 各种乱码,编码问题设置方法整理(UTF-8)

    一.tomcat中文乱码问题 打开tomcat安装目录,在conf文件夹中找到server.xml文件 ,找到   <Connector port="8009" protoc ...

  9. SQL Server 2005、SQL Server 2008版本比较

    SQL Server 2005的版本有SQL Server 2005企业版(Enterprise).SQL Server 2005标准版(Standard) 和SQL Server 2005工作组版( ...

  10. iOS8中添加的extensions总结(四)——Action扩展

    Action扩展 注:此教程来源于http://www.raywenderlich.com的<iOS8 by Tutorials> 1.准备 本次教程利用网站bitly.com进行 bit ...