题目

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) {
if (l1 == NULL)
return l2;
if (l2 == NULL)
return l1; //合并后链表初始化为空
ListNode *rl = NULL; ListNode *p = l1, *q = l2;
if (l1->val <= l2->val)
{
rl = l1;
p = l1->next;
}
else{
rl = l2;
q = l2->next;
}
rl->next = NULL;
ListNode *head = rl; while (p && q)
{
if (p->val <= q->val)
{
rl->next = p;
p = p->next;
}
else{
rl->next = q;
q = q->next;
}//else
rl = rl ->next;
}//while while (p)
{
rl->next = p;
p = p->next;
rl = rl->next;
} while (q)
{
rl->next = q;
q = q->next;
rl = rl->next;
} return head;
}
};

GitHub测试程序源码

LeetCode(21)Merge Two Sorted Lists的更多相关文章

  1. LeetCode(23)Merge k Sorted Lists

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  2. 【LeetCode算法-21】Merge Two Sorted Lists

    LeetCode第21题 Merge two sorted linked lists and return it as a new list. The new list should be made ...

  3. (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 ...

  4. LeetCode(21)题解:Merge Two Sorted Lists

    https://leetcode.com/problems/merge-two-sorted-lists/ Merge two sorted linked lists and return it as ...

  5. LeetCode之“链表”:Merge Two Sorted Lists && Merge k Sorted Lists

    1. Merge Two Sorted Lists 题目链接 题目要求:  Merge two sorted linked lists and return it as a new list. The ...

  6. [LC]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 ...

  7. leetcode第22题--Merge k Sorted Lists

    problem:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its compl ...

  8. LeetCode(88)Merge Sorted Array

    题目 Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note ...

  9. LeetCode(40)-Merge Sorted Array

    听到初爱有感 开头啰嗦两句,刚在做算法题目的时候,听到了杨宗纬的<初爱>,突然有了一种本科时候的感觉,想想自己现在研二了,青春喂了狗,我果断喝了一罐啤酒,循环这首歌到吐-.. 题目: Gi ...

随机推荐

  1. JS获取屏幕的大小

    <html><script>function a(){document.write("屏幕分辨率为:"+screen.width+"*" ...

  2. 连接php/mysql

    1 安装php5.6 首先确保mysql已安装 ./configure --prefix=/app/php-5.6.36 --with-mysql=mysqlnd --enable-mysqlnd - ...

  3. Educational Codeforces Round 19 C

    Description Petya recieved a gift of a string s with length up to 105 characters for his birthday. H ...

  4. Mirror Number SPOJ - MYQ10

    Mirror Number SPOJ - MYQ10 题意:http://blog.csdn.net/hcbbt/article/details/38349367 稍微改一下http://www.cn ...

  5. 转 PHP scandir() 函数

    实例 列出 images 目录中的文件和目录: <?php $dir = "/images/"; // 以升序排序 - 默认 $a = scandir($dir); // 以 ...

  6. 481 Magical String 魔力字符串

    详见:https://leetcode.com/problems/magical-string/description/ C++: 方法一: class Solution { public: int ...

  7. (026)[工具软件]剪切板管理:Ditto

    剪切板管理软件:Ditto官网:http://ditto-cp.sourceforge.net/

  8. ag-grid-vue的 行默认选中

    that.$nextTick(() => { that.gridListOptions.api.onGroupExpandedOrCollapsed(); that.$nextTick(() = ...

  9. android studio 生成 jniLibs 目录

    现在一般的项目都会加入第三方jar包,第三方jar包我们会新建一个文件夹:libs,然后jar包都放在这个文件夹中. 但我们会发现,只是新建一个文件加之后,在AndroidStudio的左侧并不会出现 ...

  10. RFS自动化测试(一)

    RFS 即 Robot Framework + Selenium RFS 的安装 1. python https://www.python.org/ RF框架是基于python的,所以要先安装有pyt ...