From 4a2d7f2e3fc40304ddfb647c4ec410ed89fb1517 Mon Sep 17 00:00:00 2001 From: Pratul Trivedi Date: Fri, 27 Feb 2026 08:50:37 -0800 Subject: [PATCH] Solved Linked-List 1 --- LLCycle2.py | 43 +++++++++++++++++++++++++++++++++++++ RemoveNthNodeFromEnd.py | 39 ++++++++++++++++++++++++++++++++++ ReverseLL.py | 47 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+) create mode 100644 LLCycle2.py create mode 100644 RemoveNthNodeFromEnd.py create mode 100644 ReverseLL.py diff --git a/LLCycle2.py b/LLCycle2.py new file mode 100644 index 00000000..35b92362 --- /dev/null +++ b/LLCycle2.py @@ -0,0 +1,43 @@ + # Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, x): +# self.val = x +# self.next = None + +class Solution(object): + def detectCycle(self, head): + """ + :type head: ListNode + :rtype: ListNode + """ + if not head: + return None + + slow = head + fast = head + #Let us first see if there a cycle or not. We can use fast and slow pointer approach + #for that. + isCyclePresent = False + while fast != None and fast.next != None: + slow = slow.next + fast = fast.next.next + if slow == fast: + isCyclePresent = True + break + + if not isCyclePresent: + return None + + #Now that we know that there is a cycle and fast is already in the cyclic loop, + #we can have a slow pointer start from head. While slow is not equal to fast, keep + #on incrementing both. They will collide at the cyclic point. + + slow = head + while slow != fast: + slow = slow.next + fast = fast.next + + return slow + + #TC : O(n) + #SC : O(1) \ No newline at end of file diff --git a/RemoveNthNodeFromEnd.py b/RemoveNthNodeFromEnd.py new file mode 100644 index 00000000..28464346 --- /dev/null +++ b/RemoveNthNodeFromEnd.py @@ -0,0 +1,39 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution(object): + def removeNthFromEnd(self, head, n): + """ + :type head: Optional[ListNode] + :type n: int + :rtype: Optional[ListNode] + """ + if not head: + return None + + # Dummy poitner to help us when we are trying to remove + # the head of LL. + dummy = TreeNode(-1) + dummy.next = head + slow = dummy + fast = dummy + + i = 0 + # Move the fast pointer N steps ahead. + while i < (n+1): + fast = fast.next + i += 1 + + # Until fast becomes null, we can iterate both slow and fast. + # Eventually, when fast is null, slow will be at (n-1)th node. + while fast != None: + slow = slow.next + fast = fast.next + + slow.next = slow.next.next + return dummy.next + + #Tc : O(n) + #Sc : O(1) \ No newline at end of file diff --git a/ReverseLL.py b/ReverseLL.py new file mode 100644 index 00000000..0f6528a6 --- /dev/null +++ b/ReverseLL.py @@ -0,0 +1,47 @@ +# Definition for singly-linked list. +# class ListNode(object): +# def __init__(self, val=0, next=None): +# self.val = val +# self.next = next +class Solution(object): + def reverseList(self, head): + """ + :type head: Optional[ListNode] + :rtype: Optional[ListNode] + """ + """ + Recursive Soltion : + TC : O(n) + SC : O(n) + def helper(head): + if not head.next: + return head + + re = helper(head.next) + head.next.next = head + head.next = None + return re + return helper(head) + """ + + #we can just have three pointers. prev, curr and temp to have + #store the reference of the next pointer. + prev = None + curr = head + + while curr != None: + # Store the next node is temp as we are modifying + # curr.next + temp = curr.next + # Modify curr.next to prev Node. This is basically where we + # start reversing our linked list. + curr.next = prev + + #Reversal is done. Move all the pointers one step ahead. + prev = curr + curr = temp + #when then is no node to be processed, curr will be None. Prev will be one step back, return that + return prev + + #TC : O(n) + #SC : O(1) \ No newline at end of file