https://leetcode.com/problems/merge-two-sorted-lists/

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) {
ListNode *p=new ListNode();
ListNode *q=p;
while(l1!=NULL && l2!=NULL){
if(l1->val<l2->val){
q->next=l1;
q=q->next;
l1=l1->next;
}
else{
q->next=l2;
q=q->next;
l2=l2->next;
}
}
if(l1!=NULL)
q->next=l1;
else
q->next=l2;
p=p->next;
return p;
}
};

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

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

  3. 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...

  4. [Leetcode][Python]23: Merge k Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...

  5. LeetCode LinkList 23. Merge k Sorted Lists

    这两天一直也没有顾上记录一下自己做过的题目,回头看看,感觉忘的好快,今天做了一个hard,刚开始觉得挺难得,想了两种方法,一种是每次都从k个list中选取最小的一个,为空的直接跳过,再就是每次合并其中 ...

  6. 【LeetCode练习题】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  7. leetcode 题解Merge Two Sorted Lists(有序链表归并)

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  8. [LeetCode 题解]: Merge k Sorted Lists

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

  9. 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 splicin ...

随机推荐

  1. 【LeetCode】Combination Sum II(组合总和 II)

    这道题是LeetCode里的第40道题. 题目要求: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. can ...

  2. GDB使用例子

    GDB使用例子 一般来说GDB主要调试的是C/C++的程序.要调试C/C++的程序,首先在编译时,我们必须要把调试信息加到可执行文件中.使用编译器(cc/gcc/g++)的 -g 参数可以做到这一点. ...

  3. 【JavaScript 1—基础知识点】:宏观概述

    导读:JavaScript是一门新的(也可以说是旧的或者半新语言),里面有很多的知识点都能和已有的知识产生共鸣.但是,虽然简单,相同点也有很多,也有不同点.我脑袋也不好使,所以对于我来说,还是有必要再 ...

  4. hdu 4251 The Famous ICPC Team Again划分树入门题

    The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  5. 九度oj 题目1283:第一个只出现一次的字符

    题目描述: 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符. 输入: 输入有多组数据 每一组输入一个字符串. 输出: 输出第一个只出现一次的 ...

  6. Replication and Triggers

    参考官网:https://dev.mysql.com/doc/refman/5.7/en/replication-features-triggers.html 需要了解复制和触发器关系的背景: 程序变 ...

  7. nginx反向代理+负载均衡+https

    A服务器(192.168.133.1)作为nginx代理服务器 B服务器(192.168.133.2)作为后端真实服务器 访问https://www.test.com请求从A服务器上反向代理到B服务器 ...

  8. Git 常用命令整理(持续更新)

    #配置 git config --global user.name "Your Name" git config --global user.email "email@e ...

  9. 洛谷P3097 - [USACO13DEC]最优挤奶Optimal Milking

    Portal Description 给出一个\(n(n\leq4\times10^4)\)个数的数列\(\{a_n\}(a_i\geq1)\).一个数列的最大贡献定义为其中若干个不相邻的数的和的最大 ...

  10. 算法复习——费用流模板(poj2135)

    题目: Farm Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16898   Accepted: 6543 De ...