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. Codeforces Round #265 (Div. 1)

    D. World of Darkraft - 2 time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  2. redis介绍和安装(一)

    Redis介绍:redis是一个key-value存储系统. 和Memcached类似,它支持存储的value类型相对更多,包括 string(字符串). list(链表).set(集合).zset( ...

  3. vue 自定义日历组件

    <template> <div class=""> <div class="calendarTraffic" name=" ...

  4. cell展开的几种方式

    一.插入新的cell 原理: (1)定义是否展开,和展开的cell的下标 @property (assign, nonatomic) BOOL isExpand; //是否展开 @property ( ...

  5. 洛谷P3312 - [SDOI2014]数表

    Portal Solution 共\(T(T\leq2\times10^4)\)组测试数据.给出\(n,m(n,m\leq10^5),a(a\leq10^9)\),求\[ \sum_{i=1}^n\s ...

  6. leetcode 319 灯泡问题

    例子:1-9 1的因子1 2       1,2 3        1,,3 4        1,2,4 5        1,5 6        1,2,3,6 7        1,7 8   ...

  7. *Codeforces989D. A Shade of Moonlight

    数轴上$n \leq 100000$个不重叠的云,给坐标,长度都是$l$,有些云速度1,有些云速度-1,风速记为$w$,问在风速不大于$w_{max}$时,有几对云可能在0相遇.每一对云单独考虑. 多 ...

  8. eq=等于gt=大于lt=小于的英文全称

    EQ: Equal GT: Greater Than LT: Less than 知道全称就不会忘记

  9. MySQL 游戏排行榜

    今天在坛子上看到了,顺便写下来. 有两种方法: 1.效率不高,因为有子查询.但是简洁.而且我对SOCRES表做了INDEX.所以性能上也差不了多少. mysql> show create tab ...

  10. dedecms--需要注意的细节

    在系统的系统配置参数里面修改一些参数 1:站点设置: (1):站点根网址:本地测试的话:就是你设置的虚拟主机:http://www.abc.cc (2):网页主页链接:为空 2:核心设置: DedeC ...