【leetcode】Merge Two Sorted Lists(easy)
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.
思路:使用伪头部
class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
ListNode fakehead();
ListNode *p1 = l1;
ListNode *p2 = l2;
ListNode *pnew = &fakehead;
while(p1 != NULL && p2 != NULL)
{
if(p1->val < p2->val)
{
pnew->next = p1;
p1 = p1->next;
}
else
{
pnew->next = p2;
p2 = p2->next;
}
pnew = pnew->next;
}
if(p1 != NULL)
{
pnew->next = p1;
}
if(p2 != NULL)
{
pnew->next = p2;
}
return fakehead.next;
}
};
【leetcode】Merge Two Sorted Lists(easy)的更多相关文章
- Leetcode 21. Merge Two Sorted Lists(easy)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【leetcode】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 ...
- 【leetcode】Merge k Sorted Lists(按大小顺序连接k个链表)
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)
这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...
- LeetCode:21. Merge Two Sorted Lists(Easy)
1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...
- 【leetcode】Remove Linked List Elements(easy)
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...
- 【leetcode】Contains Duplicate & Rectangle Area(easy)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
随机推荐
- 清北学堂模拟day4 业务办理
[问题描述]在银行柜台前,有 n 个顾客排队办理业务. 队伍中从前往后,第 i 位顾客办理业务需要ti 分钟时间. 一位顾客的等待时间定义为:队伍中在他之前的所有顾客和他自己的办理业务时间的总和.第 ...
- go语言mongdb管道使用
原始json: { "listsn": "", "code": "fwq_add", "detail" ...
- XDU 1161 - 科协的数字游戏II
Problem 1161 - 科协的数字游戏II Time Limit: 1000MS Memory Limit: 65536KB Difficulty: Total Submit: 112 ...
- .NET异步编程之回调
C#中异步和多线程的区别是什么呢?异步和多线程两者都可以达到避免调用线程阻塞的目的,从而提高软件的可响应性.甚至有些时候我们就认为异步和多线程是等同的概念.但是,异步和多线程还是有一些区别的.而这些区 ...
- 使用VNC登录Linux
###服务器是否配置了VNCSERVER,可以在命令行下敲入以下命令查看: [root@localhost: ~]#rpm -qa |grep vnc ###配置VNC 1. 机器IP为:10.0.0 ...
- Unity3D性能优化--- 收集整理的一堆
http://www.cnblogs.com/willbin/p/3389837.html 官方优化文档--优化图像性能http://docs.unity3d.com/Documentation/Ma ...
- Inorder Successor in Binary Search Tree
Given a binary search tree (See Definition) and a node in it, find the in-order successor of that no ...
- [转载]JavaEE学习篇之——网络传输数据中的密码学知识以及Tomcat中配置数字证书EE
原文链接:http://blog.csdn.net/jiangwei0910410003/article/details/21716557 今天是学习JavaWeb的第二天,我们来了解什么呢?就了解一 ...
- Linux中的动态库和静态库(.a/.la/.so/.o)
Linux中的动态库和静态库(.a/.la/.so/.o) Linux中的动态库和静态库(.a/.la/.so/.o) C/C++程序编译的过程 .o文件(目标文件) 创建atoi.o 使用atoi. ...
- hdu1520 树形dp Anniversary party
A - Anniversary party Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...