/ **
*
* Question:
* Analyzing single chain acyclic intersect problem
* how to determine whether the two chain acyclic intersection, the first intersection intersection node returns a disjoint null is returned.
* If the two lists intersect acyclic, then starting from the intersection node, until the termination of this period of two lists, the two chains
* table sharing.
*
* Analysis:
* 1 1 list node start from scratch, go to the last node (not the end), the statistical list length 1 is denoted len1, the same.
* 1 records the last node in the list referred to end1 time.
*
* 2. list node 2 to start from scratch, go to the last node (not the end), the statistics list the length of the note 2 is len2, with
records list the last 2 nodes recorded as end2 * time.
*
.! * 3 if end1 = end2, illustrates two disjoint lists, can return null; if end1 = end2, say
* Description intersecting two lists, proceeds to step 4 to find the first intersection node.
*
* 4. If a long list, a list len1-len2 go first step, if the chain length of 2, 2 go first chain-LEN1 LEN2
* step. Then go together two lists, go with the process, the two lists first come first node that together, is the first
* a node intersect. (Because the shared node element after intersecting, so the extra length can not intersect a certain node)
*
* For example: a list of length 100, second length of the linked list 30, if it has been determined by Step 3 lists some phases 1 and 2 list
* Pay, then the next, linked lists 1 go first 70 steps, then go with the list 1 and 2 list, they will jointly enter the first
node * intersect.
*
* @Author snow pupil
*
* /
* Code
public class Node {
public int value;
public Node next;
public Node(int data){
this.value=data;
}
}
public class NodeLoop {
Node currentHead1 = null;
Node currentHead2 = null;
int lengthNode1=0;
int lengthNode2=0;
int len = 0;
public Node nodeLoop(Node head1,Node head2){
if(head1==null&&head2==null){
return null;
}
//遍历链表
currentHead1 = head1;
while(currentHead1!=null){
lengthNode1++;
currentHead1=currentHead1.next;
}
currentHead2 = head2;
while(currentHead2!=null){
lengthNode2++;
currentHead2=currentHead2.next;
}
//相交判断
if(currentHead1 !=currentHead2){
return null;
}else{
if(lengthNode1>lengthNode2){
int flag=0;
len=lengthNode1-lengthNode2;
currentHead1=head1;
currentHead2=head2;
while(currentHead1!=currentHead2){
if(flag==len){
currentHead1=currentHead1.next;
currentHead2=currentHead2.next;
}else{
currentHead1=currentHead1.next;
}
}
return currentHead2;
}else if(lengthNode1<lengthNode2){
int flag=0;
len=lengthNode2-lengthNode1;
currentHead1=head1;
currentHead2=head2;
while(currentHead1!=currentHead2){
if(flag==len){
currentHead1=currentHead1.next;
currentHead2=currentHead2.next;
}else{
currentHead2=currentHead2.next;
}
}
return currentHead1;
}else{
return head1;
}
}
}
}
import java.util.Random;
import java.util.Scanner;
public class TestNodeLoop {
public static void main(String[] args) {
NodeLoop loop = new NodeLoop();
TestNodeLoop test = new TestNodeLoop();
Scanner sc = new Scanner(System.in);
int length1;
System.out.println("请输入链表长度:...");
length1 = sc.nextInt();
int length2;
System.out.println("请输入链表长度:...");
length2 = sc.nextInt();
Node head1;
Node head2;
Node head3;
head1=test.getNodeList(length1);
head2=test.getNodeList(length2);
test.showByTip(head1);
test.showByTip(head2);
int loopLength;
System.out.println("请输入链表相交的节点位置:...");
loopLength = sc.nextInt();
head3=test.getLoopNode(head1,head2,loopLength);
test.showByTip(head3);
Node result = loop.nodeLoop(head2, head3);
if(result!=null){
System.out.println("链表相交相交元素如下:...");
test.showByTip(result);
}else{
System.out.println("链表没有相交");
}
sc.close();
}
//获得相交链表
private Node getLoopNode(Node head1, Node head2,int length) {
int flag=0;
Node current = null;
current =head1;
while(current!=null){
flag++;
if(flag==length){
current.next=head2;
return head1;
}
current=current.next;
}
System.out.println("链表不存在可以连接的位置,请增加元素...");
return null;
}
//显示
public void showByTip(Node head) {
Node current = null;
System.out.println("链表内的元素顺序显示如下:...");
current=head;
while(current!=null) {
System.out.print(current.value+"\t");
current=current.next;
}
System.out.println("");
}
//生成链表
public Node getNodeList(int listLength) {
Random rand = new Random();
Node nodeList[]= new Node[listLength];
for(int i=0;i<listLength;i++) {
nodeList[i]= new Node(rand.nextInt(10));
}
for(int i=0;i<listLength-1;i++) {
nodeList[i].next=nodeList[i+1];
}
return nodeList[0];
}
}
*运行