Leetcode 021 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.
Java:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode p1 = l1;
ListNode p2 = l2; ListNode fakeHead = new ListNode(0);
ListNode p = fakeHead; while (p1 != null && p2 != null) { if (p1.val <= p2.val) {
p.next = p1;
p1 = p1.next;
} else {
p.next = p2;
p2 = p2.next;
} p = p.next;
} if (p1 != null) {
p.next = p1;
}
if (p2 != null) {
p.next = p2;
} return fakeHead.next;
}
}
Leetcode 021 Merge Two Sorted Lists的更多相关文章
- 【JAVA、C++】LeetCode 021 Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing ...
- [Leetcode][021] Merge Two Sorted Lists (Java)
题目在这里: https://leetcode.com/problems/merge-two-sorted-lists/ [标签]Linked List [题目分析]这个题目就是merge sort在 ...
- Java for LeetCode 023 Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...
- [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 ...
- 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]
题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- LeetCode 23 Merge k Sorted Lists(合并k个有序链表)
题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...
- [Leetcode Week4]Merge Two Sorted Lists
Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...
- [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 ...
- [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...
随机推荐
- 微信小程序新闻网站列表页
在app.json中可以设置所有文件的头部导航颜色 (是window属性的子属性) 在具体页面可以单独设置该页面的导航颜色 (直接写该属性,不需要写window属性) 查看官方文档,可以看到好多全局属 ...
- kubernetes-1.18.2集群安装-02
一.基础配置 修改主机名 # 在 172.17.32.23 上:hostnamectl set-hostname k8s-master01bash# 在 172.17.32.38 上:hostnam ...
- 内网渗透 day3 -metasploit的使用
metasploit的使用 目录 一.msf基本操作 1 二.msf模块(比较重要的四个) 2 三.木马生成 2 四.开启监听模块(最常用的模块) 3 一.msf基本操作 1. 使用msfconsol ...
- ImpalaTest
package com.niewj.demo; import java.sql.Connection; import java.sql.DriverManager; import java.sql.R ...
- W3C中不同标准的含义
学习CSS/HTML的过程中,当出现释义冲突时,W3C(万维网联盟)官网所陈列的技术标准是最核心的判断参考.但是新手在查阅W3C标准索引页面时,会发现同一个属性或者模型会出现多个不同的阶段规范展示结果 ...
- martini-实例-脂质双分子层
Martini粗粒化模型一开始就是为脂质开发的.(http://jerkwin.github.io/2016/11/03/Martini%E5%AE%9E%E4%BE%8B%E6%95%99%E7%A ...
- IDEA安装leetcode editor插件
leetcode > https://leetcode-cn.com/ 本地idea刷题可以直接同步提交,测试等相关操作 需要安装leetcode editor插件 1.idea setting ...
- 剑指offer刷题(Tree)
开篇 二刷剑指offer了,本来用Tyora记的笔记,发现字数到四万了就变得好卡o(╥﹏╥)o,刚好开始写博客,就转过来吧,记下来子自己看.不废话,开刷... JZ26. 树的子结构 输入两棵二叉树A ...
- 内核补丁热更新ceph内核模块
前言 内核模块的更新一般需要卸载模块再加载,但是很多时候使用场景决定了无法做卸载的操作,而linux支持了热更新内核模块的功能,这个已经支持了有一段时间了,一直没有拿ceph的相关模块进行验证 准备工 ...
- mysql 重要日志文件总结
作者:丁仪 来源:https://chengxuzhixin.com/blog/post/mysql_zhong_yao_ri_zhi_wen_jian_zong_jie.html 日志是所有应用的重 ...