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 together the nodes of the first two lists.
/**
* 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) {
if(l1==null)
return l2;
if(l2==null)
return l1;
ListNode headNode=new ListNode(0); //注意这里要先初始化
ListNode head=headNode;
head.next=null;
ListNode r=head,p=l1,q=l2;
while(p!=null&&q!=null){
if(p.val<=q.val){
r.next=p; //想想为什么要r.next. r不行是为什么
r=r.next;
p=p.next;
}
else{
r.next=q;
r=r.next;
q=q.next;
}
}
if(p!=null)
r.next=p;
else
r.next=q;
return head.next;
}
}
21.Merge Two Sorted Lists(链表)的更多相关文章
- Leetcode 21 Merge Two Sorted Lists 链表
合并两个已排序的链表,考到烂得不能再烂的经典题,但是很多人写这段代码会有这样或那样的问题 这里我给出了我的C++算法实现 /** * Definition for singly-linked list ...
- 21. Merge Two Sorted Lists(合并2个有序链表)
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- 21.Merge Two Sorted Lists 、23. Merge k Sorted Lists
21.Merge Two Sorted Lists 初始化一个指针作为开头,然后返回这个指针的next class Solution { public: ListNode* mergeTwoLists ...
- 21. Merge Two Sorted Lists【easy】
21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...
- leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)
1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...
- 刷题21. Merge Two Sorted Lists
一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...
- [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] 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】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
随机推荐
- DMALL刘江峰:生鲜市场具有巨大O2O改造空间
今日,全球移动互联网大会(GMIC)在北京-国家会议中心开幕.DMALL创始人刘江峰在全球O2O峰会论坛作主题发言时谈到,生鲜市场具有巨大O2O改造空间.同时这也是刘江峰在离开荣耀之后首次登台介绍自己 ...
- js下的sleep实现
function sleep(d){ for(var t = Date.now();Date.now() - t <= d;); } sleep(5000); //当前方法暂停5秒
- Web版报表的架构和设计思路
通常,我们设计一个报表,会面临设计报表,构造数据,展示报表的一个过程 设计报表也许大家都会用过,比如水晶报表,jreport一堆一堆的. 不过我觉得最好用的应该还是office做成的报表,谁都会,而且 ...
- bzoj4229: 选择
Description 现在,我想知道自己是否还有选择. 给定n个点m条边的无向图以及顺序发生的q个事件. 每个事件都属于下面两种之一: 1.删除某一条图上仍存在的边 2.询问是否存在两条边不相交的路 ...
- oracle desc 表结构
SQL> desc v$logfile; 名称 是否为空? 类型 ----------------------------------------- -------- ------------- ...
- 【Linux】之shell特殊变量整理
目录 1. 特殊变量列表 2. 特殊说明 在shell中变量名只能包含数字.字母和下划线,因为某些包含其他字符的变量有特殊含义,这样的变量被称为特殊变量. 例如,$ 表示当前Shell进程的ID,即p ...
- Java AES加密
Java AES 加密 加密 /** * * @description 加密 * * @param content 需要加密的内容 * @param password 加密密码 * @return * ...
- 关于Linux中exec的一点心得
最近在学习linux操作系统中的相关知识,在使用execlp系统调用时,发现了些有趣的东西. 首先,关于execlp函数的用法: int execlp(const char *file, const ...
- 最大熵的Java实现
这是一个最大熵的简明Java实现,提供训练与预测接口.训练采用GIS训练算法,附带示例训练集.本文旨在介绍最大熵的原理.分类和实现,不涉及公式推导或其他训练算法,请放心食用. 最大熵理论 简介 最大熵 ...
- vim 学习日志(2):set的使用方法
一,打开或关闭行号(number) 如果希望每次进入vi 会话时都显示行号,那么在主目录中的.exrc 文件中添加 set number 行. 在命令模式下,使用如下命令 :set all ...