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. php缓存小技巧

    1.缓存数组到文件: <?php $arr = array(2,3,5,76,7,8,22); $data = "<?php\nreturn ".var_export( ...

  2. ASCII码表及键盘码表。

             ASCII码表 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 ASCII值 控制字符 0 NUT 32 (space) 64 @ 96 . 1 SOH 33 ...

  3. [week4]每周总结与工作计划

    计算机网络 TAT 小白dp 28号还有一场 背单词 背马克思 python目标80% 熟悉coursera c++模版和 仿函数 人文修养 开学数据库,itercast的sql*2 itercast ...

  4. Java基础知识强化70:正则表达式之引入案例(QQ号码校验)

    1. 校验QQ号码的案例,如下: package cn.itcast_01; import java.util.Scanner; /* * 校验qq号码. * 1:要求必须是5-15位数字 * 2:0 ...

  5. linux系统应用--Linux下用virtualBox安装win7(共享文件夹)

    1. deepin终端: sudo apt-get install virtualbox 2. 下载win7 iso文件 3. deepin终端启动virtualbox   : ./virtualbo ...

  6. 关于EditText组件在android4.4W中出现黄色感叹号的问题?

    今天用eclipse编写android4.4W的项目,在XML中用到了EditText组件来写文本框,结果出现了黄色感叹号,还出现一句 This text field does not specify ...

  7. StrokeStart与StrokeEnd动画

    通过修改CAShapeLayer的StrokeStart与StrokeEnd的值来实现画图动画 效果图: 代码部分: #import "ViewController.h" @int ...

  8. [转]extern,static存储空间矛盾

    其实,这两个语句的位置不同,会出现不同的解释.这主要是由于 static 具有的两重意义所导致的: (1) 如果 static int foo; 这一句位于函数中,则 static 表示的是存储属性, ...

  9. C++中类成员使用前需要初始化的重要性

    今天写程序的时候,创建了一个结构体: struct BufferObj { char* buf; int bufLen; SOCKADDR_STORAGE addr; int addrLen; str ...

  10. Oracle数据库之创建表空间与用户

    Oracle数据库之创建表空间与用户 一.创建表空间 基本语法表述: CREATE TABLESPACE tablespace_name [DATAFILE datafile_spec1 [,data ...