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.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
中文:将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
-----------------------------------------------------------------------------------------------------------------
 
这是一个链表操作的基础题,就是注意要分配一个空间来建立一个头结点
C++代码
/**
* 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 *dummy = new ListNode(-),*cur = dummy; //* dummy是分配空间的,必须要分配空间,这个才能会得到一个结点,cur只是一个辅助结点(指针?)而已,并没有分配空间。
while(l1 && l2){
if(l1->val < l2->val){
cur->next = l1;
l1 = l1->next;
}
else{
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
cur->next = l1 ? l1:l2; //就是如果l1和l2中长度更小的遍历完后,长度更大的剩下的就由cur链接它。
return dummy->next;
}
};

(链表) 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 混合插入有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  3. [leetcode] 21. Merge Two Sorted Lists (Easy)

    合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...

  4. [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 ...

  5. 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 ...

  6. LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)

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

  7. [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 ...

  8. Java [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 spli ...

  9. # 蜗牛慢慢爬 LeetCode 21. Merge Two Sorted Lists [Difficulty: Easy]

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

随机推荐

  1. Lodop打印维护PRINT_SETUP本地缓存ini文件

    针对千差万别的客户端,Lodop提供了打印维护(PRINT_SETUP),可以针对某个客户端微调,调整结果保存在客户端本地,不会影响其他访问网站的用户的使用. 打印维护使用方法:1.PRINT_INI ...

  2. SQL to JSON Data Modeling with Hackolade

    Review: SQL to JSON data modeling First, let’s review, the main way to represent relations in a rela ...

  3. React 学习(二) ---- props验证与默认属性

    在上一节中, 我们提到了props, 组件之间数据的传递使用props. 我们调用组件时可以设置props, 组件内部通过props获取. 为了props 使用更加友好, React 提供了简单的验证 ...

  4. codeforces581C

    Developing Skills CodeForces - 581C 你在玩一个游戏.你操作的角色有n个技能,每个技能都有一个等级ai.现在你有k次提升技能的机会(将其中某个技能提升一个等级,可以重 ...

  5. Nginx 减少关闭连接的time_wait端口数量

    L:129

  6. js自动运行

    叹号后面跟函数!function 和加号后面跟函数+function 都是跟(function(){})();这个函数是一个意思,都是告诉浏览器自动运行这个匿名函数的

  7. 洛谷P1373小a和uim大逃离题解

    题目 这个题好坑啊,首先是他会卡空间,然后我们就只能把一种比较好理解的状态给舍弃,因为空间开不下,然而采用一种难理解的状态就是\(dp[i][j][l][0/1]\)表示\(i\),\(j\)位置,两 ...

  8. Matplotlib学习---用mplot3d画莫比乌斯环(Mobius strip)

    mplot3d是matplotlib里用于绘制3D图形的一个模块.关于mplot3d 绘图模块的介绍请见:https://blog.csdn.net/dahunihao/article/details ...

  9. 【XSY2709】count DP

    题目描述 有一个序列\(A\),你可以随意排列这个序列,设\(s=\sum_{i=1}^{n-1}|a_i-a_{i+1}|\) . 问你最终\(s\leq m\)的方案数有几种. 保证\(A\)中的 ...

  10. MT【308】投影的定义

    已知向量$\overrightarrow{a},\overrightarrow{b}$满足:$|\overrightarrow{a}|=2$,向量$\overrightarrow{b}$与$\over ...