标签:== ring 指针 data math pac node wal value
请实现一个函数,如果两个链表相交,请返回相交的第一个节点;如果不想交,返回null即可。
要求:如果链表1的长度为N,链表2的长度为M,时间复杂度请达到O(N+M),额外空间复杂度请达到O(1)。
package chj;
public class Problem_11_FindFirstIntersectNode {
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
public static Node getIntersectNode(Node head1, Node head2) {
if (head1 == null || head2 == null) {
return null;
}
Node loop1 = getLoopNode(head1);
Node loop2 = getLoopNode(head2);
if (loop1 == null && loop2 == null) {
return noLoop(head1, head2);
}
if (loop1 != null && loop2 != null) {
return bothLoop(head1, loop1, head2, loop2);
}
return null;
}
public static Node getLoopNode(Node head) {
if (head == null || head.next == null || head.next.next == null) {
return null;
}
Node n1 = head.next; // n1 -> slow
Node n2 = head.next.next; // n2 -> fast
while (n1 != n2) {
if (n2.next == null || n2.next.next == null) {
return null;
}
n2 = n2.next.next;
n1 = n1.next;
}
n2 = head; // n2 -> walk again from head
while (n1 != n2) {
n1 = n1.next;
n2 = n2.next;
}
return n1; //再次相交时(n1==n2),第一个入环的节点返回
}
public static Node noLoop(Node head1, Node head2) {
if (head1 == null || head2 == null) {
return null;
}
Node cur1 = head1;
Node cur2 = head2;
int n = 0;
while (cur1.next != null) {
n++;
cur1 = cur1.next;
//System.out.println("n===="+n);
//System.out.println("cur1.value="+cur1.value);
}
System.out.println("链表1遍历完时,cur1指针对应的value值:"+cur1.value);
while (cur2.next != null) {
n--;
cur2 = cur2.next;
//System.out.println("n===="+n);
//System.out.println("cur2.value="+cur2.value);
}
System.out.println("链表2遍历完时,cur2指针对应的value值:"+cur2.value);
if (cur1 != cur2) {
return null;
}
cur1 = n > 0 ? head1 : head2;
cur2 = cur1 == head1 ? head2 : head1;
//System.out.println("n的值 = "+n);
n = Math.abs(n);
while (n != 0) {
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
}
public static Node bothLoop(Node head1, Node loop1, Node head2, Node loop2) {
Node cur1 = null;
Node cur2 = null;
if (loop1 == loop2) {
cur1 = head1;
cur2 = head2;
int n = 0;
while (cur1 != loop1) {
n++;
cur1 = cur1.next;
}
System.out.println("链表1遍历完 时,cur1指针对应的value值:"+cur1.value);
while (cur2 != loop2) {
n--;
cur2 = cur2.next;
}
cur1 = n > 0 ? head1 : head2;
cur2 = cur1 == head1 ? head2 : head1;
System.out.println("n = "+n); // n = len_head1-len_head2 = 7-9 = -2
n = Math.abs(n);
while (n != 0) {
n--;
cur1 = cur1.next;
}
while (cur1 != cur2) {
cur1 = cur1.next;
cur2 = cur2.next;
}
return cur1;
} else {
cur1 = loop1.next;
while (cur1 != loop1) {
if (cur1 == loop2) {
return loop1;
}
cur1 = cur1.next;
}
return null;
}
}
public static void main(String[] args) {
// 1->2->3->4->5->6->8->12->null
Node head1 = new Node(1);
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(6);
head1.next.next.next.next.next.next = new Node(8);
head1.next.next.next.next.next.next.next = new Node(12);
// 0->9->8->6->7->null
Node head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next; // 8->6
System.out.println("Common IntersectNode: " + getIntersectNode(head1, head2).value);
//System.out.println(head2.next.next.next.next.value);
System.out.println("==========分割线==========");
// // 1->2->3->4->5->6->7->4... head1为有环链表,节点为4
head1 = new Node(1); //Node head1 =new Node(1)
head1.next = new Node(2);
head1.next.next = new Node(3);
head1.next.next.next = new Node(4);
head1.next.next.next.next = new Node(5);
head1.next.next.next.next.next = new Node(6);
head1.next.next.next.next.next.next = new Node(7);
head1.next.next.next.next.next.next = head1.next.next.next; // 7->4
//
//
// // 0->9->8->2... head2为有环节点,节点为4(其链表组成为:0->9->8->2->3->4->5->6->7->4... )
head2 = new Node(0); //Node head1 =new Node(1)
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next; // 8->2
System.out.println("head2.next.next.next.next.next的value值:"+head2.next.next.next.next.next.value);
System.out.println("--IntersectNode--: "+ getIntersectNode(head1, head2).value);
// 0->9->8->6->4->5->6.. ? head2为有环节点
head2 = new Node(0);
head2.next = new Node(9);
head2.next.next = new Node(8);
head2.next.next.next = head1.next.next.next.next.next; // 8->6
System.out.println("==IntersectNode==: "+ getIntersectNode(head1, head2).value);
System.out.println("head2.next.next.next.next.next的value值为:"+head2.next.next.next.next.next.value);
}
}
输出:
链表1遍历完时,cur1指针对应的value值:12
链表2遍历完时,cur2指针对应的value值:12
Common IntersectNode: 6
==========分割线==========
head2.next.next.next.next.next的value值:4
链表1遍历完 时,cur1指针对应的value值:4
n = -2
--IntersectNode--: 2
==IntersectNode==: 4
head2.next.next.next.next.next的value值为:5
标签:== ring 指针 data math pac node wal value
原文地址:https://www.cnblogs.com/HongjianChen/p/9413291.html