初始头结点分别为 node1、node6
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
Node node4 = new Node(4);
Node node5 = new Node(5);
Node node6 = new Node(6);
Node node7 = new Node(7);
Node node8 = new Node(8);
node1.next = node2;
node2.next = node8;
node6.next = node7;
node7.next = node4;
node8.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node3;
1、先看有环没有,没有环就是两个无环单链表相交的问题,简单!
2、若是一个有环一个没有环,那就绝不可能相交,返回null。
Node head1 = haveRing(node1);
if (head1 == null) {
System.out.println("不相交");
} else {
System.out.println("环的开始节点为:" + head1.val);
}
Node head2 = haveRing(node6);
if (head2 == null) {
System.out.println("不相交");
} else {
System.out.println("环的开始节点为:" + head2.val);
}
haveRing的两种方法
/**
* 快慢指针 空间复杂度O(1)
*/
private static Node haveRing(Node head) {
Node fast = head;
Node slow = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
fast = head;
while (fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
return null;
}
/**
* 使用hash表 空间复杂度O(n)
*/
private static Node haveRing(Node head) {
Set<Node> set = new HashSet<>();
while (head != null) {
if (set.contains(head)) {
return head;
}
set.add(head);
head = head.next;
}
return null;
}
3、都有环,就比较复杂;分两种情况:环的开始节点是否相同。
(1)、若相同,则可以转化为两个无环单链表相交的问题,只不过结束条件判空改为判断是否与开始节点相同
if (head1 == head2) {
Node head = head1;// 环开始
head1 = node1;
head2 = node6;
int n = 0;
while (head1.next != head) {
head1 = head1.next;
n++;
}
while (head2.next != head) {
head2 = head2.next;
n--;
}
if (head1 != head2) {
System.out.println("两环相交的节点为:" + head.val);
} else {
head1 = n > 0 ? node1 : node6;
head2 = n > 0 ? node6 : node1;
for (int i = 0; i < n; i++) {
head1 = head1.next;
}
while (head1 != head2) {
head1 = head1.next;
head2 = head2.next;
}
System.out.println("两环相交的节点为:" + head1.val);
}
}
(2)、若不同,分为两种情况:两链表不相交,或两链表的环相同
if (head1 != head2) {
Node head = head1;
head = head.next;
while (head != head1) {
if (head == head2) {//两链表的环相同
System.out.println("两环相交的节点为:" + head.val);
System.exit(0);
}
head = head.next;
}
//两链表不相交
System.out.println("不相交");
}
完整代码如下
import java.util.*;
/**
* 两个链表,可能有环,可能无环;可能相交,可能不相交;若相交,返回相交的第一个节点,若不相交,返回null
*
*/
public class Question13 {
public static void main(String[] args) {
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node3 = new Node(3);
Node node4 = new Node(4);
Node node5 = new Node(5);
Node node6 = new Node(6);
Node node7 = new Node(7);
Node node8 = new Node(8);
node1.next = node2;
node2.next = node8;
node6.next = node7;
node7.next = node4;
node8.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node3;
Node head1 = haveRing1(node1);
if (head1 == null) {
System.out.println("不相交");
} else {
System.out.println("环的开始节点为:" + head1.val);
}
Node head2 = haveRing2(node6);
if (head2 == null) {
System.out.println("不相交");
} else {
System.out.println("环的开始节点为:" + head2.val);
}
if (head1 != head2) {
Node head = head1;
head = head.next;
while (head != head1) {
if (head == head2) {
System.out.println("两环相交的节点为:" + head.val);
System.exit(0);
}
head = head.next;
}
System.out.println("不相交");
} else {
Node head = head1;// 环开始
head1 = node1;
head2 = node6;
int n = 0;
while (head1.next != head) {
head1 = head1.next;
n++;
}
while (head2.next != head) {
head2 = head2.next;
n--;
}
if (head1 != head2) {
System.out.println("两环相交的节点为:" + head.val);
} else {
head1 = n > 0 ? node1 : node6;
head2 = n > 0 ? node6 : node1;
for (int i = 0; i < n; i++) {
head1 = head1.next;
}
while (head1 != head2) {
head1 = head1.next;
head2 = head2.next;
}
System.out.println("两环相交的节点为:" + head1.val);
}
}
}
/**
* 快慢指针 空间复杂度O(1)
*
* @param head
* @return
*/
private static Node haveRing2(Node head) {
Node fast = head;
Node slow = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
fast = head;
while (fast != slow) {
fast = fast.next;
slow = slow.next;
}
return fast;
}
}
return null;
}
/**
* 使用hash表 空间复杂度O(n)
*
* @param head
* @return
*/
private static Node haveRing1(Node head) {
Set<Node> set = new HashSet<>();
while (head != null) {
if (set.contains(head)) {
return head;
}
set.add(head);
head = head.next;
}
return null;
}
}