LeetCode -- Merge Two Sorted Linked List
Question:
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.
Analysis:
1)两个都是空串;
2)一个是空串;
3)新建一个伪头结点,用于新串的连接;
4)最后还要检查是否有一个串还有数字。
Answer:
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode p1 = l1;
ListNode p2 = l2; if(l1 == null && l2 == null)
return null;
if(l1 == null && l2 != null)
return l2;
if(l1 != null && l2 == null)
return l1; ListNode fakeHead = new ListNode(0);
ListNode p = fakeHead; while (p1 != null && p2 != null){
if (p1.val <= p2.val) {
p.next = p1;
p1 = p1.next;
p = p.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 -- Merge Two Sorted Linked List的更多相关文章
- [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 混合插入有序链表
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 linked lists and return it as one sorted list. Analyze and describe its complexi ...
- LeetCode——Merge k Sorted Lists
Discription: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its ...
- 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 add code
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [ ...
- LeetCode Merge k Sorted Lists (链表)
题意 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- LeetCode: Merge Two Sorted Lists 解题报告
Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
随机推荐
- python的元组数据类型及常用操作
Python的元组与列表类似,不同之处在于元组的元素不能修改. 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. 如下实例: tup1 = ('physi ...
- java程序执行命令行,解锁数据库表
有些表锁的时间长或其他原因,在plsql中不能解锁,只能用命令行解锁. 有些功能跨平台系统的交互偶尔会锁表,就需要自动解锁. 下面是解锁的代码: package com.lg.BreakOracleU ...
- 查看ubuntu版本号命令
1.uname -a 查看内核版本号 2.cat /etc/issue 查看ubuntu版本号 3.sudo lsb_release -a 查看ubuntu版本号
- ctf题目writeup(1)
2019/1/28 题目来源:爱春秋 https://www.ichunqiu.com/battalion?t=1 1. 该文件是一个音频文件: 首先打开听了一下,有短促的长的....刚开始以为是摩斯 ...
- (数据科学学习手札18)二次判别分析的原理简介&Python与R实现
上一篇我们介绍了Fisher线性判别分析的原理及实现,而在判别分析中还有一个很重要的分支叫做二次判别,本文就对二次判别进行介绍: 二次判别属于距离判别法中的内容,以两总体距离判别法为例,对总体G1,, ...
- React 省市区三级联动
省市区所对应的数据来自:http://www.zgguan.com/zsfx/jsjc/6541.html react中的代码是: export default class AddReceive ex ...
- MSSQL如何查看当前数据库的连接数 【转】
- [SQL Server]版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://ai51av.blogbus.com/logs/52955622.html 如果我们发布 ...
- 【Keras案例学习】 sklearn包装器使用示范(mnist_sklearn_wrapper)
import numpy as np from keras.datasets import mnist from keras.models import Sequential from keras.l ...
- React+DvaJS 之 hook 路由权限控制
博客 学院 下载 GitChat TinyMind 论坛 APP 问答 商城 VIP 活动 招聘 ITeye 写博客 发Chat 登录注册 原 React+DvaJS 之 hook 路由权限控制 20 ...
- Unity3d脚本生命周期
如图: 测试脚本: using UnityEngine; public class Test2 : MonoBehaviour { void Awake() { Debug.Log("Awa ...