Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions LLCycle2.py
Original file line number Diff line number Diff line change
@@ -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)
39 changes: 39 additions & 0 deletions RemoveNthNodeFromEnd.py
Original file line number Diff line number Diff line change
@@ -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)
47 changes: 47 additions & 0 deletions ReverseLL.py
Original file line number Diff line number Diff line change
@@ -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)