leetCode:合併有序鏈表

認爲題目中的鏈表是指java的LinkedList的解法

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;

	}

認爲題目中的鏈表是指java的LinkedList<T extends Comparable>的解法

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;
		 }
	}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章