Website Summary Files Commits Branches @commit @diff @stats
path: root / Practicum / pract05 / solutions / Task04.cpp
diff options
context:
space:
mode:
Commit450101630aeebfb92da99417a6a71570bdfeb3e6 (patch)
Author
 Stoycho Kyosev <stoycho.kk@gmail.com>
2024-11-12 16:22:34 -0500
Committer
 Stoycho Kyosev <stoycho.kk@gmail.com>
2024-11-12 16:22:34 -0500
Files2a53c04288dd27d2e4fd639e3f409550d85b362f /Practicum/pract05/solutions/Task04.cpp
Data-structures-and-algorithms-450101630aeebfb92da99417a6a71570bdfeb3e6.tar
Data-structures-and-algorithms-450101630aeebfb92da99417a6a71570bdfeb3e6.tar.gz
Data-structures-and-algorithms-450101630aeebfb92da99417a6a71570bdfeb3e6.zip
Parentd866bd4b9fb6d586d0fbe44af9ece7d105b75a0c (diff)
Parent7ecb0c4f2407bbfe8a1300c576f0d2e4b7ef463f (diff)
Merge branch 'main' of https://github.com/stoychoX/Data-structures-and-algorithms
1 files changed, 52 insertions, 0 deletions
-rw-r--r--Practicum/pract05/solutions/Task04.cpp52
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;
+ }
+};