// 链表对齐 - 160 相交链表
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
int lenA = 0;
int lenB = 0;
ListNode * temA = headA;
ListNode * temB = headB;
while(temA != NULL)
{
temA = temA->next;
lenA ++;
}
while(temB != NULL)
{
temB = temB->next;
lenB ++;
}
//对齐
while(headA != headB && headA!= NULL && headB!= NULL)
{
if(lenA>lenB)
{
headA = headA->next;
lenA --;
}
else
{
headB = headB->next;
lenB --;
}
}
//查找相同点
if(headA== NULL || headB == NULL) return NULL;
else{
return headA;
}
}
47 - 算法 - Leetcode-160 -相交链表
最新推荐文章于 2024-04-07 16:08:05 发布