Problem: 160. 相交链表
时间复杂度: O ( n + m ) O(n+m) O(n+m)
空间复杂度:
添加空间复杂度, 示例: O ( 1 ) O(1) O(1)
/**
* 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)
{
if (headA == null || headB == null)
return null;
ListNode you = headA;
ListNode she = headB;
while (you != she)
{
// 设相交段为 C 则当走到相交段时 you ==she,不相交则走到 null
you = you == null ? headB : you.next;// A + B + C
she = she == null ? headA : she.next;// B + A + C
}
return you;
}
}