チェーンテーブルの印刷2つの順序付きチェーンテーブルの共通部分


タイトル:2つの順序付きチェーンテーブルの共通部分を印刷する
package com.chenyu.zuo.linkedList;
/**
 *            
 * @author   
 *  :            head1 head2,            
 *  :    
 *  head1    head2, head1    
 *  head2    head1, head2    
 *  head1   head2    ,      ,  head1 head2     
 *head1  head2        null,      
 */
public class PrintCommonPart {
	 public static  class Node{//   
   	  public Node next;
   	  public int value;
   	  public Node(int value){
   		  this.value=value;
   	  } 
   	  public Node(){}
     }
	/**
	 *            
	 * @param head1
	 * @param head2
	 */
	  public void printCommonPart(Node head1,Node head2){
		  if(head1==null || head2==null){
			  return ;
		  }
		  while(head1!=null&&head2!=null){
			  if(head1.value<head2.value){
				  head1=head1.next;
			  }else if(head1.value>head2.value){
				  head2=head2.next;
			  }else{
				  System.out.println("     :"+head1.value);
				  head1=head1.next;
				  head2=head2.next;
			  }
		  }
	  }
	  public static void main(String[] args) {
		  //   
		  Node firstNode=new Node(1);
		  Node  firstNode1=new Node(3);
		  Node  firstNode2=new Node(5);
		  Node  firstNode3=new Node(6);
		  Node  firstNode4=new Node(9);
		  firstNode.next= firstNode1;
		  firstNode1.next= firstNode2;
		  firstNode2.next= firstNode3;
		  firstNode3.next= firstNode4;
		  //   
		  Node secondNode=new Node(3);
		  Node  secondNode1=new Node(4);
		  Node  secondNode2=new Node(5);
		  Node  secondNode3=new Node(6);
		  Node secondNode4=new Node(9);
		  secondNode.next= secondNode1;
		  secondNode1.next= secondNode2;
		  secondNode2.next= secondNode3;
		  secondNode3.next= secondNode4;
		  PrintCommonPart pcp=new PrintCommonPart();
		  pcp.printCommonPart(firstNode, secondNode);
	} 
}

実行結果:
     :3
     :5
     :6
     :9

まとめ:
まず、テーマの重要な情報を見つける必要があります.秩序正しく、秩序正しく言えばいいです.それから、ループを終了する条件は、どのチェーンテーブルも空にすることはできません.どのチェーンテーブルの小さなポインタが下に下がるか、等しい場合は、印刷し、ポインタが下に下がることです.