Intersect lista enlazada (pensamiento)

Título:
Referencia: https://leetcode-cn.com/problems/intersection-of-two-linked-lists/solution/tu-jie-xiang-jiao-lian-biao-by-user7208t/

/**
 * 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) {
        /*
        *编写一个程序,找到两个单链表相交的起始节点
        *如果两个链表没有交点,返回 null.
        */
        ListNode *pa = headA,*pb = headB;
        while(pa != pb) {
            pa = pa ? pa->next:headB;
            pb = pb ? pb->next:headA;
        }
        return pa;
    }
};
Publicados 152 artículos originales · ganado elogios 2 · Vistas 6454

Supongo que te gusta

Origin blog.csdn.net/weixin_43918473/article/details/104672787
Recomendado
Clasificación