LeetCode: Merge Two Sorted Lists 解题报告
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.
Show Tags
SOLUTION 1:
使用dummynode记录头节点的前一个,轻松完成,2分钟就AC啦!
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode();
ListNode cur = dummy; while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
} if (l1 != null) {
cur.next = l1;
} else {
cur.next = l2;
} return dummy.next;
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/list/MergeTwoLists_1206.java
LeetCode: Merge Two Sorted Lists 解题报告的更多相关文章
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【原创】leetCodeOj --- Merge k Sorted Lists 解题报告
题目地址: https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题目内容: /** * Definition for singly-linke ...
- LeetCode Merge k Sorted Lists 解决报告
https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...
- [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 合并k个已排序的链表
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 -- 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 complexi ...
随机推荐
- ThinkPHP学习(三)
我们已经将数据保存到了后台数据库,那接下来我们肯定要将数据显示出来看看了. 先建立一个要显示数据的模板formlist.html: <!DOCTYPE HTML PUBLIC "-// ...
- 【RS】List-wise learning to rank with matrix factorization for collaborative filtering - 结合列表启发排序和矩阵分解的协同过滤
[论文标题]List-wise learning to rank with matrix factorization for collaborative filtering (RecSys '10 ...
- Python实现鸢尾花数据集分类问题——基于skearn的SVM
Python实现鸢尾花数据集分类问题——基于skearn的SVM 代码如下: # !/usr/bin/env python # encoding: utf-8 __author__ = 'Xiaoli ...
- java mongodb 基础系列---查询,排序,limit,$in,$or,输出为list,创建索引,$ne 非操作
官方api教程:http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-started ...
- MySQL 索引优化 Using where, Using filesort
用Explain分析SQL语句的时候,经常发现有的语句在Extra列会出现Using filesort,根据MySQL官方文档对他的描述: 引用 MySQL must do an extra pass ...
- Java 时间相比较
private String getRunTime() { String start = new Date.getTime(); String end= new Date.getTime(); lon ...
- rviz学习笔记(二)——Markers: Points and Lines (C++) 点和线
一.在using_marker/src中编写点和线代码 vim ~/catkin_ws/src/using_marker/src/points_and_lines.cpp 编写代码,其中有注释 #in ...
- 如何快速学会android的四大基础----Service篇
很多人都以为,只要学过一点java就可以马上写android应用了,这种想法的产生非常自然,因为现在网上有那么多的android开源实例,只要跟着来,也能够自己写一个播放器.但是,只有去写一个真正投入 ...
- matlab中的Traing、Validation、Testing
<matlab神经网络30个案例分析> ROC曲线是反映敏感性和特异性连续变量的综合指标,roc曲线真阳性率为纵坐标,假阳性率为横坐标,在坐标上由无数个临界值求出的无数对真阳性率和假阳性率 ...
- 【转】java平台的编码问题 getByte()所用编码
java平台的编码问题 getByte()所用编码 2013-09-30 11:31:22| 分类: java | 标签:java 编码 getbytes() |字号 订阅 众所周知 ...