LeetCode160——相交链表
程序员文章站
2022-06-17 19:21:52
...
题目:
思路:
代码:
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
if (headA == NULL || headB == NULL)
return NULL;
ListNode* p1 = headA;
ListNode* p2 = headB;
while (p1!=p2)
{
p1 = (p1 == NULL) ? headB : p1->next;
p2 = (p2 == NULL) ? headA : p2->next;
}
return p1;
}
};
下一篇: JSON简介