diff options
Add tasks solutions
1 files changed, 52 insertions, 0 deletions
| -rw-r--r-- | Practicum/pract05/solutions/Task04.cpp | 52 |
diff --git a/Practicum/pract05/solutions/Task04.cpp b/Practicum/pract05/solutions/Task04.cpp new file mode 100644 index 0000000..3fc11a5 --- /dev/null +++ b/Practicum/pract05/solutions/Task04.cpp @@ -0,0 +1,52 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + size_t getLen(ListNode* head) + { + size_t cnt = 0; + while(head) + { + head = head->next; + cnt++; + } + + return cnt; + } + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + if(!headA || !headB) + return nullptr; + + size_t sizeA = getLen(headA); + size_t sizeB = getLen(headB); + + while(sizeA > sizeB) + { + headA = headA->next; + sizeA--; + } + + while(sizeB > sizeA) + { + headB = headB->next; + sizeB--; + } + + while(headA && headB) + { + if(headA == headB) + return headA; + + headA = headA->next; + headB = headB->next; + } + + return nullptr; + } +}; |
