Leetcode/cpp/160a.cpp
2024-06-21 01:36:27 +01:00

29 lines
717 B
C++

/**
* Definition for singly-linked list.
*/
#include <cstddef>
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
// This solution is not great, being O(n^2)-ish time, although it is O(1) space, which is good.
// There is likely at least an O(n) in both solution...
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
while (headA) {
ListNode* check = headB;
while (check) {
if (check == headA) {
return headA;
}
check = check->next;
}
headA = headA->next;
}
return nullptr;
}
};