leetcode 腾讯精选160

题目:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/comments/
代码:
/**

  • 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 *pa = headA;
    ListNode *pb= headB;

     if(pa==NULL||pb == NULL)
         return NULL;
     
    while(pa!= pb)
    {
        pa = pa!=NULL?pa->next:headB;
        pb = pb!=NULL?pb->next:headA;
        
    }
     
     return pa;
    

    }
    };

你可能感兴趣的:(c++)