From 312d7f8c6b6456d759db3eac363a6483dac17f33 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Thu, 20 Jan 2022 10:31:40 -0700 Subject: [PATCH 01/10] Create reverse_linked_list --- reverse_linked_list | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 reverse_linked_list diff --git a/reverse_linked_list b/reverse_linked_list new file mode 100644 index 00000000..f3e08b03 --- /dev/null +++ b/reverse_linked_list @@ -0,0 +1,28 @@ +class Solution: + + # Time Complexity - O(n) + # Space Complexity - O(1) + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + if head==None or head.next==None: + return head + prev = None + curr = head + far = head.next + while far!=None: + curr.next = prev + prev = curr + curr = far + far = far.next + curr.next = prev + return curr + + ''' + # Time Complexity - O(n) + # Space Complexity - O(n) + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + if head==None or head.next==None: + return head + temp = self.reverseList(head.next) + head.next.next = head + head.next = None + return temp''' From bc4abe7bb29ea38994e289a68fbb04b3b7350f1e Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Thu, 20 Jan 2022 10:31:52 -0700 Subject: [PATCH 02/10] Rename reverse_linked_list to reverse_linked_list.py --- reverse_linked_list => reverse_linked_list.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename reverse_linked_list => reverse_linked_list.py (100%) diff --git a/reverse_linked_list b/reverse_linked_list.py similarity index 100% rename from reverse_linked_list rename to reverse_linked_list.py From f0876a6f8bd8c19ab7bdd2d266806a3cb7401048 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Sat, 12 Mar 2022 11:54:59 -0700 Subject: [PATCH 03/10] Create remove nth node from end of LL.py --- remove nth node from end of LL.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 remove nth node from end of LL.py diff --git a/remove nth node from end of LL.py b/remove nth node from end of LL.py new file mode 100644 index 00000000..109877e4 --- /dev/null +++ b/remove nth node from end of LL.py @@ -0,0 +1,15 @@ +# Time Complexity - O(n) +# Space Complexity - O(1) +class Solution: + def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + dummy = ListNode(-1) + dummy.next = head + s = dummy + f = dummy + for i in range(n): + f = f.next + while f.next!=None: + s = s.next + f = f.next + s.next = s.next.next + return dummy.next From 233c7cb3662f9a7345f4901ed4144237a4d7805d Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Sat, 12 Mar 2022 12:09:44 -0700 Subject: [PATCH 04/10] Create linked list cycle 2.py --- linked list cycle 2.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 linked list cycle 2.py diff --git a/linked list cycle 2.py b/linked list cycle 2.py new file mode 100644 index 00000000..557b0825 --- /dev/null +++ b/linked list cycle 2.py @@ -0,0 +1,22 @@ +# Time Complexity = O(n) +# Space Complexity - O(1) +class Solution: + def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: + if head==None: + return None + flag = False + s = head + f = head + while f!=None and f.next!=None: + s = s.next + f = f.next.next + if f==s: + flag=True + f = head + break + if not flag: + return None + while f!=s: + f=f.next + s=s.next + return s From 5b7a4ff92243e00ea6aa86085662942709309747 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Fri, 1 May 2026 14:37:53 -0700 Subject: [PATCH 05/10] Delete reverse_linked_list.py --- reverse_linked_list.py | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 reverse_linked_list.py diff --git a/reverse_linked_list.py b/reverse_linked_list.py deleted file mode 100644 index f3e08b03..00000000 --- a/reverse_linked_list.py +++ /dev/null @@ -1,28 +0,0 @@ -class Solution: - - # Time Complexity - O(n) - # Space Complexity - O(1) - def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: - if head==None or head.next==None: - return head - prev = None - curr = head - far = head.next - while far!=None: - curr.next = prev - prev = curr - curr = far - far = far.next - curr.next = prev - return curr - - ''' - # Time Complexity - O(n) - # Space Complexity - O(n) - def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: - if head==None or head.next==None: - return head - temp = self.reverseList(head.next) - head.next.next = head - head.next = None - return temp''' From 64a0247cd02fd16a027e0df0dc3af8cf4c33280c Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Fri, 1 May 2026 14:38:00 -0700 Subject: [PATCH 06/10] Delete remove nth node from end of LL.py --- remove nth node from end of LL.py | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 remove nth node from end of LL.py diff --git a/remove nth node from end of LL.py b/remove nth node from end of LL.py deleted file mode 100644 index 109877e4..00000000 --- a/remove nth node from end of LL.py +++ /dev/null @@ -1,15 +0,0 @@ -# Time Complexity - O(n) -# Space Complexity - O(1) -class Solution: - def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: - dummy = ListNode(-1) - dummy.next = head - s = dummy - f = dummy - for i in range(n): - f = f.next - while f.next!=None: - s = s.next - f = f.next - s.next = s.next.next - return dummy.next From e573a744372f4665b49d1a20e8d0a315055a8894 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Fri, 1 May 2026 14:38:07 -0700 Subject: [PATCH 07/10] Delete linked list cycle 2.py --- linked list cycle 2.py | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 linked list cycle 2.py diff --git a/linked list cycle 2.py b/linked list cycle 2.py deleted file mode 100644 index 557b0825..00000000 --- a/linked list cycle 2.py +++ /dev/null @@ -1,22 +0,0 @@ -# Time Complexity = O(n) -# Space Complexity - O(1) -class Solution: - def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: - if head==None: - return None - flag = False - s = head - f = head - while f!=None and f.next!=None: - s = s.next - f = f.next.next - if f==s: - flag=True - f = head - break - if not flag: - return None - while f!=s: - f=f.next - s=s.next - return s From 14f292af49bbf32381fd71563fc54e417c8ee3e5 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Fri, 1 May 2026 14:57:37 -0700 Subject: [PATCH 08/10] Create Problem1.py --- Problem1.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Problem1.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..3fb5af75 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,18 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next + +# Time Complexity --> O(n) +# Space Complexity --> O(1) +class Solution: + def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: + prev = None + curr = head + while curr is not None: + temp = curr.next + curr.next = prev + prev = curr + curr = temp + return prev From 6f85f4527d65d8c5ec1e59907fd35d4ec9c312cb Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Fri, 1 May 2026 16:13:40 -0700 Subject: [PATCH 09/10] Create Problem2.py --- Problem2.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 Problem2.py diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..8cda742f --- /dev/null +++ b/Problem2.py @@ -0,0 +1,27 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next + +# Time Complexity --> O(n) +# Space Complexity --> O(1) +class Solution: + def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: + dummy = ListNode(None) + dummy.next = head + slow, fast = dummy, dummy + + count = 0 + while count<=n: + count += 1 + fast = fast.next + + while fast is not None: + fast = fast.next + slow = slow.next + temp = slow.next + slow.next = slow.next.next + temp.next = None + + return dummy.next From 8dd56e2783b5dd8b805dca43f3cb555a26329bd2 Mon Sep 17 00:00:00 2001 From: "M.Hitesh" <54840773+hiteshmadapathi@users.noreply.github.com> Date: Fri, 1 May 2026 16:27:07 -0700 Subject: [PATCH 10/10] Add cycle detection in linked list Implement Floyd's Tortoise and Hare algorithm to detect cycle in a linked list. --- Problem3.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Problem3.py diff --git a/Problem3.py b/Problem3.py new file mode 100644 index 00000000..35cf72c6 --- /dev/null +++ b/Problem3.py @@ -0,0 +1,26 @@ +# Definition for singly-linked list. +# class ListNode: +# def __init__(self, x): +# self.val = x +# self.next = None + +# Time Complexity --> O(n) +# Space Complexity --> O(1) +class Solution: + def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: + slow = head + fast = head + + while fast is not None and fast.next is not None: + slow = slow.next + fast = fast.next.next + if slow == fast: + break + + if fast is None or fast.next is None: + return None + fast = head + while slow!=fast: + slow = slow.next + fast = fast.next + return slow