public static void main(String[] args){
Integer [] a = {3,5,9,12,14,18,21};
Integer [] b = {2,3,7,15,19,22};
LinkedList<Integer> lista = new LinkedList<Integer>(Arrays.asList(a));
LinkedList<Integer> listb = new LinkedList<Integer>(Arrays.asList(b));
try{
merge(lista,listb);
}catch(Exception e){
e.printStackTrace();
}
}
static void merge(LinkedList<Integer> a,LinkedList<Integer> b) throws Exception{
if( !(ordered(a) && ordered(b)) ) throw new Exception("illegal param!");
out.println("合并前a:"+a);
out.println("合并前b:"+b);
ListIterator<Integer> aiter = a.listIterator();
ListIterator<Integer> biter = b.listIterator();
while(aiter.hasNext()){
int acurr = aiter.next();
int bcurr=0;
while(biter.hasNext() ){
bcurr = biter.next();
if(acurr<bcurr){
biter.previous();
break;
}
}
biter.add(acurr);
}
if(!ordered(b)) throw new Exception("合并失败,合并后的链表不是有序的");
out.println("合并后b:"+b);
}
/**检测链表是否是正序有序的
*/
static boolean ordered(LinkedList<Integer> list){
ListIterator<Integer> iter = list.listIterator();
int pre=iter.next();
while(iter.hasNext()){
int curr = iter.next();
if(curr<pre) return false;
pre = curr;
}
return true;
}
public static void main(String[] args){
Pair [] a = {new Pair(3,5),new Pair(9,12),new Pair(14,18)};
Pair [] b = {new Pair(2,3),new Pair(7,15),new Pair(19,22)};
LinkedList<Pair> lista = new LinkedList<Pair>(Arrays.asList(a));
LinkedList<Pair> listb = new LinkedList<Pair>(Arrays.asList(b));
try{
merge(lista,listb);
}catch(Exception e){
e.printStackTrace();
}
}
static <T extends Comparable<T>> void merge(LinkedList<T> a,LinkedList<T> b) throws Exception{
out.println("merge");
if( !(ordered(a) && ordered(b)) ) throw new Exception("illegal param!");
out.println("合并前a:"+a);
out.println("合并前b:"+b);
ListIterator<T> aiter = a.listIterator();
ListIterator<T> biter = b.listIterator();
while(aiter.hasNext()){
T acurr = aiter.next();
T bcurr=null;
while(biter.hasNext() ){
bcurr = biter.next();
if(acurr.compareTo(bcurr)<0){
biter.previous();
break;
}
}
biter.add(acurr);
}
if(!ordered(b)) throw new Exception("合并失败,合并后的链表不是有序的");
out.println("合并后b:"+b);
}
/**检测链表是否是正序有序的
*/
static <T extends Comparable<T>> boolean ordered(LinkedList<T> list){
ListIterator<T> iter = list.listIterator();
T pre=iter.next();
while(iter.hasNext()){
T curr = iter.next();
if(curr.compareTo(pre)<0) return false;
pre = curr;
}
return true;
}
static class Pair implements Comparable<Pair>{
private int first;
private int second;
Pair(int first,int second){
this.first = first;
this.second = second;
}
public int getFirst(){
return first;
}
public int getSecond(){
return second;
}
public int compareTo(Pair other){
if(other ==null) return 1;
return Integer.compare((first+second),(other.first+other.second));
}
public String toString(){
return new StringBuilder(getClass().toString()).append("[first:").append(first).append(",second:").append(second).append("]").toString();
}
}
个人感觉自定义链表的解法更轻松简单,用java的LinkedList的ListIterator的next和previous的解法很费劲儿,可能还是由于自己对API用的不怎么熟练。
static <T extends Comparable<T>> ListNode merge(ListNode<T> first,ListNode<T> second){
if(first==null) return second;
if(second==null) return first;
ListNode<T> merged = null;
ListNode<T> curr=null;
ListNode<T> fn = first;
ListNode<T> sn = second;
do{
ListNode<T> min = fn.getValue().compareTo(sn.getValue())<0 ? fn : sn;
if(Objects.isNull(merged)){
merged = min;
curr = min;
}
else{
curr.setNext(min);
curr=min;
}
if(min == fn){
fn = fn.getNext();
} else{
sn = sn.getNext();
}
curr.setNext(null);
}while(Objects.nonNull(fn) && Objects.nonNull(sn));
if(Objects.nonNull(fn)){curr.setNext(fn);}
else if(Objects.nonNull(sn)){curr.setNext(sn);}
return merged;
}
static class ListNode<T extends Comparable<T>>
{
private T value;
private ListNode<T> next;
private ListNode<T> pre;
public ListNode(T value){
this.value = value;
}
public T getValue(){
return value;
}
public void setNext(ListNode<T> next){
this.next = next;
}
public void setPre(ListNode<T> pre){
this.pre = pre;
}
public ListNode<T> getNext(){
return next;
}
public ListNode<T> getPre(){
return pre;
}
}