From f281f4124941da875a81df70b9b91b6eaf529434 Mon Sep 17 00:00:00 2001 From: Desislava Shunina <115353472+desiish@users.noreply.github.com> Date: Tue, 5 Nov 2024 17:12:04 +0200 Subject: Add tasks solutions --- Practicum/pract05/solutions/Task01.cpp | 17 +++++++++++ Practicum/pract05/solutions/Task03.cpp | 18 ++++++++++++ Practicum/pract05/solutions/Task04.cpp | 52 ++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 Practicum/pract05/solutions/Task01.cpp create mode 100644 Practicum/pract05/solutions/Task03.cpp create mode 100644 Practicum/pract05/solutions/Task04.cpp (limited to 'Practicum/pract05/solutions') diff --git a/Practicum/pract05/solutions/Task01.cpp b/Practicum/pract05/solutions/Task01.cpp new file mode 100644 index 0000000..1f9e7a9 --- /dev/null +++ b/Practicum/pract05/solutions/Task01.cpp @@ -0,0 +1,17 @@ +SinglyLinkedListNode* reverse(SinglyLinkedListNode* llist) { + if(!llist) + return nullptr; + + SinglyLinkedListNode* curr = llist; + SinglyLinkedListNode* prev = nullptr; + + while(curr) + { + SinglyLinkedListNode* next = curr->next; + curr->next = prev; + prev = curr; + curr = next; + } + + return prev; +} diff --git a/Practicum/pract05/solutions/Task03.cpp b/Practicum/pract05/solutions/Task03.cpp new file mode 100644 index 0000000..42282c6 --- /dev/null +++ b/Practicum/pract05/solutions/Task03.cpp @@ -0,0 +1,18 @@ +bool has_cycle(SinglyLinkedListNode* head) { + if(!head) + return false; + + SinglyLinkedListNode* slow = head; + SinglyLinkedListNode* fast = head; + + while(fast && fast->next) + { + slow = slow->next; + fast = fast->next->next; + + if(slow == fast) + return true; + } + + return false; +} 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; + } +}; -- cgit v1.3.1-23-gd82d From b445b1b7522eb8862dffd027f391ee224e25b241 Mon Sep 17 00:00:00 2001 From: Desislava Shunina <115353472+desiish@users.noreply.github.com> Date: Tue, 5 Nov 2024 17:13:22 +0200 Subject: add second task solution --- Practicum/pract05/solutions/Task02.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Practicum/pract05/solutions/Task02.cpp (limited to 'Practicum/pract05/solutions') diff --git a/Practicum/pract05/solutions/Task02.cpp b/Practicum/pract05/solutions/Task02.cpp new file mode 100644 index 0000000..4905d58 --- /dev/null +++ b/Practicum/pract05/solutions/Task02.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + ListNode* middleNode(ListNode* head) { + if(!head) + return nullptr; + + ListNode* middle = head; + ListNode* end = head; + + while(end != nullptr && end->next != nullptr) { + middle = middle->next; + end = end->next->next; + } + return middle; + } +}; -- cgit v1.3.1-23-gd82d