题目

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. Hexo瞎折腾系列(2) - 添加背景图片轮播

    动态背景图片插件jquery-backstretch jquery-backstretch是一款简单的jQuery插件,可以用来设置动态的背景图片,以下是官方网站的介绍. A simple jQuer ...

  2. c++关键字explicit

    关键字explicit,可以阻止不应该允许的经过转换构造函数进行的隐式转换的发生.声明为explicit的构造函数不能在隐式转换中使用. C++中, 一个参数的构造函数(或者除了第一个参数外其余参数都 ...

  3. JQUERY 获取 DIV 宽度与高度(width,padding,margin,border)

    一般讲的宽度指的是内容宽度,但一个 div 的实际宽度不仅只于内容宽度,尤其在做 CSS 排版时更不能搞错,必须同时考虑 Padding.Border 与  Margin 的宽度,四个加起来才是 di ...

  4. Qt容器类之二:迭代器

    一.介绍 遍历一个容器可以使用迭代器(iterators)来完成,迭代器提供了一个统一的方法来访问容器中的项目.Qt的容器类提供了两种类型的迭代器:Java风格迭代器和STL风格迭代器.如果只是想按顺 ...

  5. LightOj 1170 - Counting Perfect BST (折半枚举 + 卡特兰树)

    题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1170 题目描述: 给出一些满足完美性质的一列数(x > 1 and y ...

  6. 洛谷p2922[USACO08DEC]秘密消息Secret Message

    题目: 题目链接:[USACO08DEC]秘密消息Secret Message 题意: 给定n条01信息和m条01密码,对于每一条密码A,求所有信息中包含它的信息条数和被它包含的信息条数的和. 分析: ...

  7. Educational Codeforces Round 20 B

    Description You are given the array of integer numbers a0, a1, ..., an - 1. For each element find th ...

  8. adb的含义

    ADB全名Andorid Debug Bridge. 是一个Debug工具.为何称之为Bridge呢?因为adb是一个标准的C/S结构的工具, 是要连接开发电脑和调试手机的.包含如下几个部分: 1.C ...

  9. 155 Min Stack 最小栈

    设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈.    push(x) -- 将元素x推入栈中.    pop() -- 删除栈顶的元素.    top() -- 获取 ...

  10. Java中“==”的使用,以及“==”和equal的比较

    int i02=59 ,这是一个基本类型,存储在栈中. Integer i03 =Integer.valueOf(59); 因为 IntegerCache 中已经存在此对象,所以,直接返回引用. In ...