leetcode — two-sum-ii-input-array-is-sorted
import java.util.ArrayList;
import java.util.List;
/**
* Source : https://oj.leetcode.com/problems/merge-two-sorted-lists/
*
* Created by lverpeng on 2017/7/11.
*
* 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.
*/
public class MergeTwoSortedList {
/**
* 把两个链表合并到一个新的链表中
* 保存新的链表头head,使用一个新链表的指针的current用来指向新链表的尾部,用来添加新的元素
*
* 最后加入较长链表的其余元素
*
* @param list1
* @param list2
* @return
*/
public Node merge (Node list1, Node list2) {
Node head = null;
Node current = null;
while (list1 != null && list2 != null) {
Node n = null;
if (list1.value < list2.value) {
n = list1;
list1 = list1.next;
} else {
n = list2;
list2 = list2.next;
}
if (head == null) {
head = current = n;
} else {
current.next = n;
current = current.next;
}
}
list1 = list1 == null ? list2 : list1;
if (list1 != null) {
if (head != null && current != null) {
current.next = list1;
} else {
head = list1;
}
}
return head;
}
private static class Node {
int value;
Node next;
@Override
public String toString() {
return "Node{" +
"value=" + value +
", next=" + (next == null ? "" : next.value) +
'}';
}
}
private static void print (Node node) {
while (node != null) {
System.out.println(node);
node = node.next;
}
}
public static void main(String[] args) {
MergeTwoSortedList mergeTwoSortedList = new MergeTwoSortedList();
Node list1 = new Node();
Node pointer1 = list1;
list1.value = 1;
Node list2 = new Node();
list2.value = 2;
Node pointer2 = list2;
for (int i = 3; i < 10; i++) {
Node node = new Node();
node.value = i;
if (i % 2 == 1) {
pointer1.next = node;
pointer1 = pointer1.next;
} else {
pointer2.next = node;
pointer2 = pointer2.next;
}
}
print(list1);
System.out.println();
print(list2);
System.out.println();
Node result = mergeTwoSortedList.merge(list1, list2);
print(result);
}
}
leetcode — two-sum-ii-input-array-is-sorted的更多相关文章
- [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- LeetCode Two Sum II - Input array is sorted
原题链接在这里:https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ 题目: Given an array of intege ...
- leetcode Two Sum II - Input array is sorted <面试常考题>
题目描述 //二分查找的变形 用头尾两个指针进行 面试考察题 class Solution { public: vector<int> twoSum(vector<int> ...
- Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)
Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...
- 167. Two Sum II - Input array is sorted - LeetCode
Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...
- 29. leetcode 167. Two Sum II - Input array is sorted
167. Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascendi ...
- 【LEETCODE】38、167题,Two Sum II - Input array is sorted
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- LeetCode_167. Two Sum II - Input array is sorted
167. Two Sum II - Input array is sorted Easy Given an array of integers that is already sorted in as ...
- leetcode2 Two Sum II – Input array is sorted
Two Sum II – Input array is sorted whowhoha@outlook.com Question: Similar to Question [1. Two Sum], ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
随机推荐
- springboot中使用ContextLoaderListener.getCurrentWebApplicationContext();获取WebApplicationContext为空问题
WebApplicationContext applicationContext = ContextLoaderListener.getCurrentWebApplicationContext(); ...
- C++中的仿函数
仿函数:实质就是重载了小括号(),通过类,定义一个对象,对象可以被实例化,具有内存可以存储数据,把需要比较的数据事先给到类对象的成员,这样在比较两个值的时候,可以只传入需要被比较的值即可.因为比较的值 ...
- 记一次Java Core Dump分析过程
#背景提要 很久没有亲自动手部署代码了,命令行的亲切感越来越低.放飞了键盘,习惯了鼠标操作的windows环境.冷不丁实操部署也是不错的. 常常在部署时,运维同学对于[hs_err_pid]文件视而不 ...
- leveldb 学习记录(五)SSTable格式介绍
本节主要记录SSTable的结构 为下一步代码阅读打好基础,考虑到已经有大量优秀博客解析透彻 就不再编写了 这里推荐 https://blog.csdn.net/tankles/article/det ...
- tcp、ip、http
tcp是传输层协议,ip是网络层协议,http是应用层协议,简单说就是tcp是传输数据,而http是封装数据. rpc与http的区别是项目大的话,接口间调用变多的话,采用rpc的话,不用像http那 ...
- Python11/19--MySQL的基本使用
1.什么是数据库 存放数据的仓库 已学习的文件操作的缺陷 1.IO操作 效率问题 2.多用户竞争数据 3.网络访问 4.用户的验证2.常见的数据库 关系型 数据之间存在某种关联关系 oracle 目前 ...
- python模块:xml.dom.minidom
"""Simple implementation of the Level 1 DOM. Namespaces and other minor Level 2 featu ...
- IDEA中MyBaits的Mapper文件颜色问题
IDEA中MyBaits的Mapper文件颜色问题 在IDEA中Mapper文件的展示 包含的警告及其解决方案 然后我们就完成了,效果如下 在IDEA中Mapper文件的展示 在IDEA中,Mappe ...
- Jsp+Struts2+JavaBean+DAO开发模式(1)
DAO模式就实现了把数据库表的操作转化对Java类的操作,从而提高程序的可读性,并实现更改数据库的方便性.其架构图如下图. 一共分为五个组件(component) jsp提交页面(一下四其中的一个例子 ...
- yum-内网yum源服务器配置(CentOS6.5)
一.安装apache服务1.安装httpd服务 yum -y install httpd (纯内网用rpm包安装也可以) 2.启动httpd服务 service httpd start 二.挂载完整的 ...