链表交叉点

请写一个程序,找到两个单链表最开始的交叉节点。
如果两个链表没有交叉,返回null。
在返回结果后,两个链表仍须保持原有的结构。
可假定整个链表结构中没有循环。
样例
下列两个链表:


A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗            
B:     b1 → b2 → b3
在节点 c1 开始交叉。


挑战 

需满足 O(n) 时间复杂度,且仅用 O(1) 内存。

import java.util.Scanner;

/**
 * 请写一个程序,找到两个单链表最开始的交叉节点。

 注意事项

如果两个链表没有交叉,返回null。
在返回结果后,两个链表仍须保持原有的结构。
可假定整个链表结构中没有循环。
样例
下列两个链表:

A:          a1 → a2
                   ↘
                     c1 → c2 → c3
                   ↗            
B:     b1 → b2 → b3
在节点 c1 开始交叉。

挑战 
需满足 O(n) 时间复杂度,且仅用 O(1) 内存。
 * 这里提供了两种方法getIntersectionNode和getIntersectionNode1。
 * @author Dell
 *
 */
class ListNode{
	int val;
	ListNode next;
	ListNode(int x)
	{
		val=x;
		next=null;
	}
	
}
public class Test380 {
  public static ListNode getIntersectionNode(ListNode headA, ListNode headB)
  {
	  if(!isjiaocha1(headA,headB))
		  return null;
	  int a=getLength(headA);
	  int b=getLength(headB);
	ListNode p=headA.next;
	ListNode q=headB.next;
	if(a>b)
	{
       int t=a-b;
       int temp=1;
       while(temp<=t)
       {
    	   p=p.next;
    	   temp++;
       }
       
 	}
	else if(a<b)
	{
		int t=b-a;
	       int temp=1;
	       while(temp<=t)
	       {
	    	   q=q.next;
	    	   temp++;
	       }	
	}
	  
	while(p!=q)
	{
		p=p.next;
		q=q.next;
	}
	return p;
  }
  public static int getLength(ListNode head)
  {
	  ListNode p=head.next;
	  int count=0;
	  while(p!=null)
	  {
		  count++;
		  p=p.next;
	  }
	 return count; 
  }
  public static boolean isjiaocha1(ListNode headA, ListNode headB)
  {
	     if(headA==null||headA.next==null||headB==null||headB.next==null)
	       return false;
	     ListNode p=headA.next;
	     ListNode r=headA.next;
	     ListNode q=headB.next;
	     while(p!=null)
	     {  
	    	 r=p;
	       p=p.next;
	     }
	    p=headB.next;
	    while(p!=null)
	    {
	    	q=p;
	    	p=p.next;
	    }
	  if(q==r)
		  return true;
	  else
		  return false;	    
  }
  public static ListNode getIntersectionNode1(ListNode headA, ListNode headB)
  {
	  if(headA==null||headA.next==null||headB==null||headB.next==null)
	       return null;
	  ListNode p=headA.next;
	  ListNode r=headA.next;
	  ListNode t=headA.next;
	  while(p!=null)
	  {
		 r=p;
		 p=p.next;  
	  }
	 r.next=headB.next;
	ListNode lian=iscycle(headA);
     if(lian==null)
    	 return null;
     else{
     while(lian!=t)
     {
    	 lian=lian.next;
        t=t.next;
    	 
     }
     return t;
     }
  }
  public static ListNode iscycle(ListNode head)
  {
	   if(head==null||head.next==null)
		   return null;
	  ListNode slow=head.next;
	  ListNode fast=head.next;
	  while(fast.next!=null&&fast.next.next!=null)
	  {
		  slow=slow.next;
		  fast=fast.next.next; 
		  if(slow==fast)
			return slow;
	  }
    return null;
	  
  }
	public static void main(String[] args) {
	     Scanner sc=new Scanner(System.in);
	     int n1=sc.nextInt();
	     int n2=sc.nextInt();
	   ListNode headA=new ListNode(-1);
	   ListNode p=headA;
	   ListNode headB=new ListNode(-1);
	   ListNode q=headB;
	   for(int i=0;i<n1;i++)
	   {
		   ListNode temp=new ListNode(sc.nextInt());
		   p.next=temp;
		   p=p.next;
	   }
	   q.next=headA.next;
	 /*  ListNode r=headA.next.next.next;
	   ListNode temp=new ListNode(6);
	   ListNode temp1=new ListNode(7);
	   ListNode temp2=new ListNode(8);
	   q.next=temp;
	   q=q.next;
	   q.next=temp1;
	   q=q.next;
	   q.next=temp2;
	   q=q.next;
	   q.next=r;*/
	  /* for(int i=0;i<n2;i++)
	   {
		   ListNode temp=new ListNode(sc.nextInt());
		   q.next=temp;
		   q=q.next;
	   } */
	     
	  ListNode result=getIntersectionNode1(headA,headB);
	  if(result==null)
	  {
		  System.out.println("wu");
	  }
	  else
		  System.out.println(result.val);

	}

}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值