仅作为备注, 便于自己回顾.

import java.util.Arrays;

public class MergeSort {
public static class LinkedNode<V extends Comparable<V>> {
public V value;
public LinkedNode<V> next; public LinkedNode(V value) {
this.value = value;
}
public LinkedNode(V value, LinkedNode<V> next) {
this.value = value;
this.next = next;
}
} public static <T extends Comparable<T>> LinkedNode<T> prepareSortedLinkedList(T...list) {
Arrays.sort(list);
LinkedNode<T> head = new LinkedNode<>(list[0]);
LinkedNode<T> begin = head;
for (int i = 1; i < list.length; i++) {
begin.next = new LinkedNode<>(list[i]);
begin = begin.next;
}
return head;
} public static <T extends Comparable<T>> LinkedNode<T> cloneLinkedList(LinkedNode<T> head) {
if (head == null) {
return null;
}
LinkedNode<T> result = new LinkedNode<>(head.value);
LinkedNode<T> clonedList = result;
while (true) {
head = head.next;
if (head != null) {
clonedList.next = new LinkedNode<>(head.value);
clonedList = clonedList.next;
} else {
break;
}
}
return result;
} public static <T extends Comparable<T>> void dumpLinkedList(LinkedNode<T> head) {
while (true) {
if (head != null) {
System.out.print(String.valueOf(head.value) + ' ');
} else {
break;
}
head = head.next;
}
System.out.println();
} public static void main(String[] args) {
LinkedNode<String> head11 = prepareSortedLinkedList("dump", "just", "found", "get");
LinkedNode<String> head21 = cloneLinkedList(head11);
dumpLinkedList(head11);
System.out.println("-------------------------------------------");
dumpLinkedList(head21);
System.out.println("-------------------------------------------");
LinkedNode<String> head12 = prepareSortedLinkedList("get", "zara", "yes", "but", "row", "ok", "another");
LinkedNode<String> head22 = cloneLinkedList(head12);
dumpLinkedList(head12);
System.out.println("-------------------------------------------");
dumpLinkedList(head22);
// end prepare
System.out.println("\n++++++++++++++++++++++++++++++++++++++++++\n");
// start test
LinkedNode<String> result1 = mergeSortedLinkedList1(head11, head12);
dumpLinkedList(result1);
System.out.println("-------------------------------------------");
LinkedNode<String> result2 = mergeSortedLinkedList2(head21, head22);
dumpLinkedList(result2);
} /** recusive. return head node as ascending. will change parameters, non reentrant. */
public static <T extends Comparable<T>> LinkedNode<T> mergeSortedLinkedList1(LinkedNode<T> a, LinkedNode<T> b) {
LinkedNode<T> head;
if (a == null) {
return b;
}
if (b == null) {
return a;
}
if (isFirstLessThanSecond(a.value, b.value)) {
head = a;
head.next = mergeSortedLinkedList1(a.next, b);
} else {
head = b;
head.next = mergeSortedLinkedList1(a, b.next);
}
return head;
} /** virtual node + loop. return head node as ascending. will change parameters, non reentrant. */
public static <T extends Comparable<T>> LinkedNode<T> mergeSortedLinkedList2(LinkedNode<T> a, LinkedNode<T> b) {
LinkedNode<T> header = null, head = null;
if (a == null) {
return b;
}
if (b == null) {
return a;
}
while (true) {
if (isFirstLessThanSecond(a.value, b.value)) {
if (head == null) {
header = head = a;
} else {
head.next = a;
head = head.next;
}
a = a.next;
} else {
if (head == null) {
header = head = b;
} else {
head.next = b;
head = head.next;
}
b = b.next;
}
if (a == null) {
head.next = b;
break;
} else if (b == null) {
head.next = a;
break;
}
}
return header;
} /**
* 1. null is smallest;
* 2. if first == null && second == null then return true;
* 3. if first equals second then return false;
*/
private static <T extends Comparable<T>> boolean isFirstLessThanSecond(T first, T second) {
if (first == null) {
return true;
}
if (second == null) {
return false;
}
return first.compareTo(second) < 0;
}
}
 
 

两个有序单链表合并成一个有序单链表的java实现的更多相关文章

  1. 两个有序数组合并成一个有序数组(要求时间复杂度为O(n))

    面试题: 怎样把两个有序数组合并成有序数组呢 逻辑步骤: 1.假设两个数组为A和B 2.A和B都是从小到大的顺序进行排列 ** 1.我们可以直接比较两个数组的首元素,哪个小就把这个小元素放入可变数组. ...

  2. 合并k个有序数组

    给定K个有序数组,每个数组有n个元素,想把这些数组合并成一个有序数组 可以利用最小堆完成,时间复杂度是O(nklogk),具体过程如下: 创建一个大小为n*k的数组保存最后的结果创建一个大小为k的最小 ...

  3. 2、java数据结构和算法:单链表: 反转,逆序打印, 合并二个有序链表,获取倒数第n个节点, 链表的有序插入

    什么也不说, 直接上代码: 功能点有: 1, 获取尾结点 2, 添加(添加节点到链表的最后面) 3, 添加(根据节点的no(排名)的大小, 有序添加) 4, 单向链表的 遍历 5, 链表的长度 6, ...

  4. c# 有序链表合并 链表反转

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. [LeetCode] Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...

  6. 【LeetCode每天一题】 Merge k Sorted Lists(合并K个有序链表)

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  7. java合并两个升序数组为一个新的有序数组

    转自:http://blog.csdn.net/laozhaokun/article/details/37531247 题目:有两个有序数组a,b,现需要将其合并成一个新的有序数组. 简单的思路就是先 ...

  8. [剑指offer] 14. 链表中倒数第K个节点+翻转+逆序打印+合并两个排序链表 + 链表相交(第一个公共节点) (链表)

    题目描述 输入一个链表,输出该链表中倒数第k个结点. 思路:  两个指针,起始位置都是从链表头开始,第一个比第二个先走K个节点,当第一个走到链表尾时,第二个指针的位置就是倒数第k个节点.(两指针始终相 ...

  9. [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

随机推荐

  1. 在谷歌浏览器中安装防广告的插件(abp)

    1.打开谷歌浏览器 2.打开 设置-->见到"扩展程序"--->在搜索框中搜索"adb"-->点击"Adblock plus&quo ...

  2. 【Unity】4.4 添加角色控制器

    分类:Unity.C#.VS2015 创建日期:2016-04-10 一.简介 设计完毕基本的场景后,一般都需要先运行看看效果如何,即先让场景"动起来",以方便观察不同的位置,而不 ...

  3. 查看Android内存的8中方法

    方法一: 通过手机上Running services的Activity查看,可以通过Setting->Applications->Running services进. 关于Running ...

  4. WebClient请求帮助类

    /// <summary> /// 通过JSON方式发送POST请求 /// 将返回结果按JSON方式解析 /// </summary> public static class ...

  5. socket.io笔记一

    //服务端代码 var server = require('http').createServer(app); var io = require('socket.io')(server,{path:' ...

  6. spark 分区

    http://stackoverflow.com/questions/39368516/number-of-partitions-of-spark-dataframe

  7. RightScale 2019年云状态报告:公共云快速增长 微软Azure增长最快

    https://www.rightscale.com/ 全球云管理服务厂商RightScale发布了年度云状态报告,今年报告的十大主要内容包括:企业在多云平台上投入巨资.公共云继续快速增长,但是私有云 ...

  8. 每日英语:Foreign Tourists Skip Beijing

    Overseas tourists continued to shun Beijing through 2013. shun:避开,避免,回避 Amid rising pollution and a ...

  9. cocos2dx+lua注册事件函数详解 事件

    coocs2dx 版本 3.1.1 registerScriptTouchHandler             注册触屏事件 registerScriptTapHandler             ...

  10. C# IOCP服务器项目(学习)

    无论什么平台,编写支持高并发性的网络服务器,瓶颈往往出在I/O上,目前最高效的是采用Asynchronous I/O模型,Linux平台提供了epoll,Windows平台提供了I/O Complet ...