java——单链表的交

单链表的交:

java——单链表的交
java——单链表的交
java——单链表的交

    /**
     * 创建交点
     * @param t1
     * @param t2
     */
     public static void CreateCut(Node t1,Node t2) {
            Entry head1 = t1.head; 
            Entry head2 = t2.head; 
            head1.next.next =  head2.next;

    }


      /**
       * 判断是否相交
       * @param t1
       * @param t2
       * @return
       */
      public static boolean isCut(Node t1,Node t2) {
            Node.Entry head1 = t1.head; //获得两个链表的头结点
            Node.Entry head2 = t2.head; 
            int len1 = t1.getLength();          //获取到两个链表长度
            int len2 = t2.getLength();  
            int mylen = len1-len2;              //找到两个链表的差值
            if(mylen<0)  {                       //如果mylen小于0代表t2比t1长
                head1 = t2.head;           //此时换一下位置即可,保证head1为最长元素的头结点
                head2 = t1.head;
            }
            for (int i = 0; i <mylen; i++) {    //对长的先进行遍历
                head1 = head1.next;
            }
           //进行循环,直到两个链表找到交点,或者遍历完成 
            while(head1 != null && head2 != null && head1 != head2) {  
                head1 = head1.next;
                head2 = head2.next;
            }
            //如果找到交点 返回true 负责为false
            if(head1 == head2 && head1 != null && head2 != null) {
                  return true;
            }
            else {
                return false;
            }
        }


      /**
       * 找到交点
       * @param t1
       * @param t2
       * @return
       */
      public static int isCutPoint(Node t1,Node t2) {
            Node.Entry head1 = t1.head; 
            Node.Entry head2 = t2.head; 
            int len1 = t1.getLength();
            int len2 = t2.getLength();
            int mylen = len1-len2;
            if(mylen<0) {
                head1 = t2.head;
                head2 = t1.head;
            }
            for (int i = 0; i <mylen; i++) {
                head1 = head1.next;
            }
            while(head1 !=null && head2 !=null && head1 != head2) {
                head1 = head1.next;
                head2 = head2.next;
            }
            if(head1 == head2 && head1 !=null && head2 != null){
                return head1.data;
            }
            else {
                return -1;
            }
        }

单链表的环
两个有序链表的合并