【leetcode】Merge Two Sorted Lists(easy)

时间:2022-09-15 16:45:48

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)的更多相关文章

  1. Leetcode 21&period; 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 ...

  2. 【leetcode】Merge k Sorted Lists

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

  3. 【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 ...

  4. 【leetcode】Merge k Sorted Lists&lpar;按大小顺序连接k个链表&rpar;

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

  5. 【LeetCode】Merge Two Sorted Lists&lpar;合并两个有序链表&rpar;

    这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...

  6. LeetCode:21&period; Merge Two Sorted Lists(Easy)

    1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...

  7. 【leetcode】Remove Linked List Elements(easy)

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  8. 【leetcode】Contains Duplicate &amp&semi; Rectangle Area(easy)

    Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...

  9. 【LeetCode】120&period; Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

随机推荐

  1. 云计算的三种服务模式&colon;SaaS&sol;PaaS&sol;IaaS

    转载http://blog.chinaunix.net/uid-22414998-id-3141499.html 定义 云计算主要分为三种服务模式,而且这个三层的分法重要是从用户体验的角度出发的: S ...

  2. &lbrack;转&rsqb;表结构设计器EZDML介绍说明(包含修改配置文件,修改文本字段属性)

    超轻量级的表结构设计工具,这是一个数据库建表的小软件,可快速的进行数据库表结构设计,建立数据模型.类似大家常用的数据库建模工具如PowerDesigner.ERWIN.ER-Studio和Ration ...

  3. 在Android中使用Java 8的lambda表达式

    作为一名Java开发者,或许你时常因为缺乏闭包而产生许多的困扰.幸运的是:Java's 8th version introduced lambda functions给我们带来了好消息;然而,这咩有什 ...

  4. Node&period;js与Sails~方法拦截器policies

    回到目录 policies sails的方法拦截器类似于.net mvc里的Filter,即它可以作用在controller的action上,在服务器响应指定action之前,对这个action进行拦 ...

  5. optiontransferselect例子

    Struts2 OptionTransferSelect标签 动态赋值: 1.html片面: <td class="td2"> <s:optiontransfer ...

  6. HDU 1496 Equations 等式(二分&plus;暴力,技巧)

    题意:给出4个数字a,b,c,d,求出满足算式a*x1^2+b*x2^2+c*x3^2+d*x4^2=0的 (x1,x2,x3,x4) 的组合数.x的范围[-100,100],四个数字的范围 [-50 ...

  7. Groovy读取文件信息

    1. eachLine -- 打开和读取文件的每一行 new File("foo.txt").eachLine { println it.toUpperCase(); } 2. r ...

  8. 【BZOJ1924】【SDOI2010】所驼门王的宝藏(Tarjan,SPFA)

    题目描述 在宽广的非洲荒漠中,生活着一群勤劳勇敢的羊驼家族.被族人恭称为"先知"的Alpaca L. Sotomon是这个家族的领袖,外人也称其为"所驼门王". ...

  9. Git 实用指南

    传送门: https://mp.weixin.qq.com/s?__biz=MzI3MzgxNDY2MQ==&mid=2247484159&idx=1&sn=2d28513ef ...

  10. 开发中少不了的Fun -- 获取地址栏URL参数

    假设这是一个url地址 http://localhost:8080/a/b/c?a=1&b=2#abc,里面包含的部分: protocol: 'http:', // 协议 host: 'loc ...