【剑指offer】【链表】52.两个链表的第一个公共节点
题目链接:https://leetcode-cn.com/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/
双指针
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *A = headA, *B = headB;
while(A != B){
if(A) A = A -> next;
else A = headB;
if(B) B = B -> next;
else B = headA;
}
return A;
}
};
知识的价值不在于占有,而在于使用

浙公网安备 33010602011771号