请写一个程序,找到两个单链表最开始的交叉节点。
如果两个链表没有交叉,返回null。
在返回结果后,两个链表仍须保持原有的结构。
可假定整个链表结构中没有循环。
样例
下列两个链表:
A: a1 → a2
↘
c1 → c2 → c3
↗
B: b1 → b2 → b3
在节点 c1 开始交叉。
挑战
如果两个链表没有交叉,返回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);
}
}