29 lines
717 B
C++
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;
|
|
}
|
|
};
|