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. JavaScript RegExp.$1

    我们不生产代码 我们只是代码的搬运工 JavaScript RegExp.$1 RegExp 是javascript中的一个内置对象.为正则表达式. RegExp.$1是RegExp的一个属性,指的是 ...

  2. Python3基础知识之运算符

    题:今天学习python运算符,学完了回头看看与.net和java有什么异同. 目标:学习了解运算符,学会一般的应用. 相关知识: Python语言支持以下类型的运算符: 算术运算符 比较(关系)运算 ...

  3. maven +bootstrap+ssm

    http://blog.csdn.net/yangwenxue_admin/article/details/71757505

  4. Java:ConcurrentHashMap

    ConcurrentHashMap的目的 多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap.虽然已经有一个线程安全的Ha ...

  5. MFC字体样式和颜色设置

    在编写MFC界面程序时,可能会使用不同大小或者颜色的字体,这里稍做记录. 使用方法 CFont *m_pFont;//创建新的字体 m_pFont = new CFont; m_pFont->C ...

  6. LOJ-10100(割点个数)

    题目链接:传送门 思路: 就是求割点的个数,直接Tarjan算法就行. 注意输入格式(判断比较水). #include<iostream> #include<cstdio> # ...

  7. Codeforces Round #539 (Div. 2) D 思维

    https://codeforces.com/contest/1113/problem/D 题意 将一个回文串切成一段一段,重新拼接,组成一个新的回文串,问最少切几刀 题解 首先无论奇偶串,最多只会切 ...

  8. 第一周Access课总结

    一.问:这节课学到了什么知识? 答:这周课程迎来新的学习领域,作为初次学Access有了一定的了解,Access是office办公软件中的一个极为重要的组成部分,它可以对大量的数据进行存储,查找,统计 ...

  9. EF6学习笔记(四) 弹性连接及命令拦截调试

    EF6学习笔记总目录:ASP.NET MVC5 及 EF6 学习笔记 - (目录整理) 本章原文地址:Connection Resiliency and Command Interception 原文 ...

  10. Spring Boot Actuator 使用

    转载于:https://www.jianshu.com/p/af9738634a21 Spring Boot 的 Actuator 提供了很多生产级的特性,比如监控和度量Spring Boot 应用程 ...