From f49d160db6f0220609724b9c3eb49427c4da2e4e Mon Sep 17 00:00:00 2001 From: Akansha2202 <50712974+Akansha2202@users.noreply.github.com> Date: Sun, 3 Oct 2021 20:15:27 +0530 Subject: [PATCH 1/3] Adding Leetcode problem number 142 solution --- C++/142_Linked_List_Cycle_II.cpp | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 C++/142_Linked_List_Cycle_II.cpp diff --git a/C++/142_Linked_List_Cycle_II.cpp b/C++/142_Linked_List_Cycle_II.cpp new file mode 100644 index 0000000..0d2a23e --- /dev/null +++ b/C++/142_Linked_List_Cycle_II.cpp @@ -0,0 +1,43 @@ +//Problem Link -: https://leetcode.com/problems/linked-list-cycle-ii/ + + +//Solution -: + +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode *detectCycle(ListNode *head) { + if (head == NULL or head -> next == NULL or head->next->next == NULL) { + return NULL; + } + ListNode *p1 = head; + ListNode *p2 = head; + int flag = 0; + while (p1 != NULL and p2 != NULL and p2 -> next != NULL) { + p1 = p1->next; + p2 = p2->next->next; + if (p1 == p2) { + flag++; + break; + } + } + if (flag == 0) { + return NULL; + } + else { + p1 = head; + while (p1 != p2) { + p1 = p1->next; + p2 = p2->next; + } + return p1; + } + } +}; \ No newline at end of file From 5c2a3d4df6d7b59d102ef267de705d4dc83bea1f Mon Sep 17 00:00:00 2001 From: Akansha2202 <50712974+Akansha2202@users.noreply.github.com> Date: Sun, 3 Oct 2021 20:16:44 +0530 Subject: [PATCH 2/3] Rename 142_Linked_List_Cycle_II.cpp to 142-Linked_List_Cycle_II.cpp --- ...42_Linked_List_Cycle_II.cpp => 142-Linked_List_Cycle_II.cpp} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename C++/{142_Linked_List_Cycle_II.cpp => 142-Linked_List_Cycle_II.cpp} (99%) diff --git a/C++/142_Linked_List_Cycle_II.cpp b/C++/142-Linked_List_Cycle_II.cpp similarity index 99% rename from C++/142_Linked_List_Cycle_II.cpp rename to C++/142-Linked_List_Cycle_II.cpp index 0d2a23e..8c442e1 100644 --- a/C++/142_Linked_List_Cycle_II.cpp +++ b/C++/142-Linked_List_Cycle_II.cpp @@ -40,4 +40,4 @@ class Solution { return p1; } } -}; \ No newline at end of file +}; From 652d50b448900c79b8df7a0cd2dc2e876bbee73d Mon Sep 17 00:00:00 2001 From: Akansha2202 <50712974+Akansha2202@users.noreply.github.com> Date: Sun, 3 Oct 2021 20:18:13 +0530 Subject: [PATCH 3/3] Update 142-Linked_List_Cycle_II.cpp --- C++/142-Linked_List_Cycle_II.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/C++/142-Linked_List_Cycle_II.cpp b/C++/142-Linked_List_Cycle_II.cpp index 8c442e1..5f3b953 100644 --- a/C++/142-Linked_List_Cycle_II.cpp +++ b/C++/142-Linked_List_Cycle_II.cpp @@ -1,5 +1,10 @@ //Problem Link -: https://leetcode.com/problems/linked-list-cycle-ii/ +// Given the head of a linked list, return the node where the cycle begins. If there is no cycle, return null. + +// There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to (0-indexed). It is -1 if there is no cycle. Note that pos is not passed as a parameter. + +// Do not modify the linked list. //Solution -: