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. Linux安装redis服务器

    Linux安装redis服务器 初次接触,这里简单的说下我遇到的情况以及安装方法,当然也是参考了诸位大神的. 确定虚拟机的主机IP. 1)首先需要一个linux虚拟机,确定虚拟机的ip ,输入命令:# ...

  2. RN与webview通讯

     一.RN给webview发送信息 this.webview.postMessage(message) 二.监听从React Native发过来的消息: window.document.addEven ...

  3. css杂烩

    持续更新... 在css中处理外边距合并时,思路是触发BFC,因为在创建了块级格式化的元素中(垂直排列),不会和它的子元素发生外边距合并.而BFC可以通过float(不包括none),overflow ...

  4. PHPNow升级PHP版本

    PHPNow升级PHP版本 phpnow下载地址:http://www.jb51.net/softs/12868.html 1,先把PHP5.3.5下载下来,在官网我是没找到VC6的版本,只能从Goo ...

  5. oracle 区分大小写遇到的坑

    1. oracle 字段是区分大小写的 ..在navicat 中使用查询 select REMAIN_PRINCIPAl from T_NF_PROJECT;    navicat 默认会把 REMA ...

  6. JavaScript 高阶函数

    高阶函数的英文叫Higher-order function ,什么是高阶函数呢>? JavaScript的函数其实都指向某个变量.既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接 ...

  7. tabel表格的dom操作

    对table表格的操作比较麻烦,一直字符串的连接会比较麻烦 var nod = cwgk_msg_list.insertRow();//这个是上边获取过的dom元素,一般是tbody的元素,对元素进行 ...

  8. lambda 匿名函数

    # 普通python函数 def func(a,b,c): return a+b+c print func(1,2,3) # 返回值为6 # lambda匿名函数 f = lambda a,b,c:a ...

  9. Unity3D中声音播放

    Unity3D 播放声音需要使用 Audio Source 组件,并且需要 Audio Listener 组件配合,不然无法听到声音.Main Camera 会默认有 Audio Lisetener. ...

  10. Linux 线程编程2.0——线程同步-互斥锁

    当我们需要控制对共享资源的存取的时候,可以用一种简单的加锁的方法来控制.我们可以创建一个读/写程序,它们共用一个共享缓冲区,使用互斥锁来控制对缓冲区的存取. 函数 pthread_mutex_init ...