解法
package com.wangxiaohu;import java.util.HashSet;public class LeetCode160 {
ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode p1 = headA, p2 = headB;while (p1 != p2) {
if (p1 == null) {
p1 = headB;} else {
p1 = p1.next;}if (p2 == null) {
p2 = headA;} else {
p2 = p2.next;}}return p1;}ListNode getIntersectionNode2(ListNode headA, ListNode headB) {
ListNode p1 = headA, p2 = headB;while (p1 != p2) {
p1 = p1 == null ? headB : p1.next;p2 = p2 == null ? headA : p2.next;}return p1;}ListNode getIntersectionNode3(ListNode headA, ListNode headB) {
HashSet<ListNode> set = new HashSet<>();ListNode p1 = headA;while (p1 != null && p1.next != null) {
if (!set.contains(p1)) {
set.add(p1);p1 = p1.next;}}ListNode p2 = headB;while (p2 != null && p2.next != null) {
if (set.contains(p2)) {
return p2;} else {
p2 = p2.next;}}return null;}public class ListNode {
int val;ListNode next;ListNode() {
}ListNode(int val) {
this.val = val;}ListNode(int val, ListNode next) {
this.val = val;this.next = next;}}
}