【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 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) {
ListNode head=new ListNode(-1);
ListNode addNode=head;
while(l1!=null&&l2!=null){
if(l1.val<l2.val){
addNode.next=l1;
l1=l1.next;
}else{
addNode.next=l2;
l2=l2.next;
}
addNode=addNode.next;
}
if(l1==null) addNode.next=l2;
if(l2==null) addNode.next=l1;
return head.next;
}
}
【LeetCode】21. Merge Two Sorted Lists的更多相关文章
- 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...
- 【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 spli ...
- 【LeetCode】23. Merge k Sorted Lists 合并K个升序链表
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,链表,单链表,题解,leetcode, 力扣,Py ...
- 【一天一道LeetCode】#21. Merge Two Sorted Lists
一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...
- 【LeetCode】023. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...
- 【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 t ...
- 【LeetCode】23. Merge k Sorted Lists
合并k个已合并链表. 思路:先把链表两两合并,直到合并至只有一个链表 /** * Definition for singly-linked list. * struct ListNode { * in ...
- [Leetcode][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- C# 写 LeetCode easy #21 Merge Two Sorted Lists
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
随机推荐
- 剑指offer系列45---和为s的两个数字
[题目]输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S, package com.exe9.offer; /** * [题目]输入一个递增排序的数组和一个数字S,在数组中 ...
- Windows2012修改光驱盘符
1.输入diskmgmt.msc打开磁盘管理器 2.找到需要修改的盘符,右键点击修改盘符
- 【freemaker】之循环,判断,对象取值
entity: public class Employee { private Integer id; private String name; private Integer age; privat ...
- makefile详解 嵌套执行make,定义命令包
嵌套执行make 在一些大的工程中,我们会把我们不同模块或是不同功能的源文件放在不同的目录中,我们可以在每个目录中都书写一个该目录的Makefile,这有利于让我们的Makefile变得更加地简洁,而 ...
- 在eclipse中设计BPMN 2.0工作流定义的根本步骤
原文地址:http://www.myexception.cn/eclipse/1863140.html 在eclipse中设计BPMN 2.0工作流定义的基本步骤 1. Activiti问我们提供了A ...
- 实现web数据同步的四种方式
http://www.admin10000.com/document/6067.html 实现web数据同步的四种方式 1.nfs实现web数据共享 2.rsync +inotify实现web数据同步 ...
- override 修饰符
override(C# 参考) 要扩展或修改继承的方法.属性.索引器或事件的抽象实现或虚实现,必须使用 override 修饰符. C# abstract class ShapesClass { ab ...
- 黄聪:Mysql5.6缓存命中率
MySQL缓存命中率,网上说法不一,下面我说下我的看法,大家轻拍: 总的select查询数等于com_select(没命中) + qcache_hits(命中) + 解析错误的查询. 再来看看Com_ ...
- 黄聪:Discuz X2.5、3.0、3.1、3.2 如何不用插件实现用户名只允许中文注册
1.在后台--注册与访问--注册链接文字,把“注册”改为“中文注册”或“注册(请使用中文注册)”等 2.后台UCenter管理中心---注册设置---禁止的用户名: *q* *w* *e* * ...
- Linux中变量$#,$@,$0,$1,$2,$*,$$,$?的含义(转载)
From:http://dadekey.blog.51cto.com/107327/119938 我们先写一个简单的脚本,执行以后再解释各个变量的意义 # touch variable # vi ...