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. 573 The Snail(蜗牛)

      The Snail  A snail is at the bottom of a 6-foot well and wants to climb to the top. The snail can ...

  2. hdu1387之queue应用

    Team Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  3. 使用MFC读写Excel

    _Application m_ExlApp;   //组件服务器的各个classes     _Workbook m_ExlBook;     Workbooks m_ExlBooks;     _W ...

  4. 重叠I/O之事件通知

      在 Winsock 中,重叠 I/O(Overlapped I/O)模型能达到更佳的系统性能,高于select模型.异步选择和事件选择三种.重叠模型的基本设计原理便是让应用程序使 用一个重叠的数据 ...

  5. RSA加密解密及数字签名Java实现--转

    RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院 ...

  6. 关于js跨域

    get方式: 称为jsonp,就是js的跨域通信方式,因为知道有些标签可以跨域获取内容,例如img,script,link...,jsonp就是把动态创建一个script标签,然后配置src属性,后台 ...

  7. sass笔记-3|Sass基础语法之样式复用和保持简洁

    上一篇详述了Sass如何嵌套.导入和注释这3个基本方式来保持条理性和可读性,这一篇更进一步地阐述sass保持样式复用和简洁的方式--混合器和选择器继承--这两种方式都能复用样式,使用它们也不难,但一定 ...

  8. linux的常用命令及常用快捷键

    常用指令 ls        显示文件或目录 -l           列出文件详细信息l(list) -a          列出当前目录下所有文件及目录,包括隐藏的a(all) mkdir     ...

  9. csms发布步骤

    1.wcf发布 通过点击 CSMS2.Application,右键发布按钮,将文件拷贝 2.打包文件 CSMS2.Resources 中 update.config 修改为对应 ServerUrl地址 ...

  10. jquery网站左侧弹出导航菜单

    下载