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的更多相关文章

  1. [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 ...

  2. LeetCode Two Sum II - Input array is sorted

    原题链接在这里:https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/ 题目: Given an array of intege ...

  3. leetcode Two Sum II - Input array is sorted <面试常考题>

    题目描述 //二分查找的变形   用头尾两个指针进行  面试考察题 class Solution { public: vector<int> twoSum(vector<int> ...

  4. Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    Leetcode之二分法专题-167. 两数之和 II - 输入有序数组(Two Sum II - Input array is sorted) 给定一个已按照升序排列 的有序数组,找到两个数使得它们 ...

  5. 167. Two Sum II - Input array is sorted - LeetCode

    Question 167. Two Sum II - Input array is sorted Solution 题目大意:和Two Sum一样,这里给出的数组是有序的 思路:target - nu ...

  6. 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 ...

  7. 【LEETCODE】38、167题,Two Sum II - Input array is sorted

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  8. 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 ...

  9. 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], ...

  10. 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 ...

随机推荐

  1. 符合Chrome58的证书制作

    Chrome 58开始取消了对通用名检查的支持, 但网上大多数OpenSSL使用教程没有提及这一点, 制作出的证书总是提示ERR_CERT_COMMON_NAME_INVALID 错误, 所以分享出解 ...

  2. JVM 字节码(三)异常在字节码中的处理(catch 和 throws)

    JVM 字节码(三)异常在字节码中的处理(catch 和 throws) 在 ClassFile 中到底是如何处理异常的呢? 一.代码块异常 catch catch 中的异常代码块在异常是如何处理的呢 ...

  3. linux 查看信息-磁盘分区&网络

    磁盘和分区 1.查看挂接的分区状态 2.查看所有交换分区 3.查看启动时IDE设备检测状况 网络 1.查看网络接口属性 2.查看防火墙设置 3.查看路由表 4.查看所有监听端口 5.查看所有已经建立的 ...

  4. 【Git】 GitLab配置优化及汉化

    GitLab配置 1.修改GitLab绑定的域名 a.修改/etc/gitlab/gitlab.rb配置文件,修改成自己的域名 external_url 'http://gitlab.example. ...

  5. URL与URI的区别和联系

    转自:https://www.cnblogs.com/chengdabelief/p/6635045.html 简单理解是这样的:理解URI和URL的区别,我们引入URN这个概念. URI = Uni ...

  6. js-闪烁的文字

    <!DOCTYPE html><html>    <head lang="en">        <meta charset=" ...

  7. 利用Go2Shell 实现 Mac Finder 直接shell端打开当前文件夹

    Finder 窗口 ,点击下图所示的按钮(红色框内),即可打开Shell Terminal. 打开后,如图 用法 安装go2shell后,打开finder的application文件夹,找到go2sh ...

  8. visual studio单项目一次生成多框架类库、多框架项目合并

    目录 不同平台框架项目使用同一套代码,一次编译生成多个框架类库 需要先了解的东西 分析 添加PropertyGroup 多目标平台 编译符号和输出目录设置 添加依赖 代码文件处理 主副平台项目文件处理 ...

  9. unity 简单通用游戏模式设计

    好吧好吧,又谈到这个问题了,其实早就想写这个博客了,犹豫了好久.在设计游戏的时候我本人是很排斥什么游戏架构设计,mvc什么的,我只想马上动手就把自己的游戏玩法最快的用代码敲出来,还不会出无法挽回的错误 ...

  10. 解决“UnicodeEncodeError: 'gbk' codec can't encode character u'\xa9' in position 24051: illegal multibyte sequence”错误

    今天我在爬取一个网页时出现了下面这个错误: UnicodeEncodeError: 'gbk' codec can't encode character u'\xa9' in position 240 ...