思路
一开始以为要固定A,然后循环B,这个复杂度也太高了
题解:
https://leetcode.cn/problems/intersection-of-two-linked-lists/?envType=study-plan-v2&envId=top-100-liked
代码
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) {* val = x;* next = null;* }* }*/
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode pa = headA;ListNode pb = headB;int flag = 0;while (true){if (pa == null){flag++;pa = headB;}if (pb == null){flag++;pb = headA;}if (flag == 2){break;}pa = pa.next;pb = pb.next;}while (true){if (pa == pb){return pa;}pa = pa.next;pb = pb.next;}}
}