题目

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. hdu6201 transaction transaction transaction(from 2017 ACM/ICPC Asia Regional Shenyang Online)

    最开始一直想着最短路,不过看完题解后,才知道可以做成最长路.唉,还是太菜了. 先上图: 只要自己添加两个点,然后如此图般求最长路即可,emmm,用SPFA可以,迪杰斯特拉也可以,或者别的都ok,只要通 ...

  2. C/C++预处理

    C/C++编译系统编译程序的过程为预处理.编译.链接.预处理器是在程序源文件被编译之前根据预处理指令对程序源文件进行处理的程序.预处理器指令以#号开头标识,末尾不包含分号.预处理命令不是C/C++语言 ...

  3. php连接mysql并读取数据

    <?php $server_name="localhost:3306"; //数据库服务器名称 $username="root"; // 连接数据库用户名 ...

  4. 清橙A1339. JZPLCM(顾昱洲)

    http://www.tsinsen.com/ViewGProblem.page?gpid=A1339 题解:https://blog.csdn.net/LOI_DQS/article/details ...

  5. SpringMVC的Controller的返回值与接收的参数

    内容参考自博客: http://blog.csdn.net/u011001084/article/details/52846791 http://blog.csdn.net/xuxiaoyinliu/ ...

  6. 增强的for循环

  7. Object类的几个方法

    1.protected Object clone()创建并返回此对象的一个副本. 2. boolean equals(Object obj)指示其他某个对象是否与此对象“相等”. 3. protect ...

  8. Retrofit Upload multiple files and parameters

    Retrofit 的介绍以及基本使用 这里不再说明. 关于多文件上传 以及上传文件的同时携带多个参数说明 网上涉及到的不是太多. 上一张帅图: 代码: apiService: /** params 参 ...

  9. mysql对库,表及记录的增删改查

    破解密码 #1.关闭mysqlnet stop mysqlmysql还在运行时需要输入命令关闭,也可以手动去服务关闭 #2.重新启动mysqld --skip-grant-tables跳过权限 #3m ...

  10. CF779A(round 402 div.2 A) Pupils Redistribution

    题意: In Berland each high school student is characterized by academic performance — integer value bet ...