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
39 changes: 39 additions & 0 deletions BSTIterator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Time Complexity : O(1)
# Space Complexity : O(h)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach : Use lazy evaluation by only pushing left children into the stack.

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class BSTIterator:

def __init__(self, root: Optional[TreeNode]):
self.stack = []
self.dfs(root)

def dfs(self, root: Optional[TreeNode]):
while root:
self.stack.append(root)
root = root.left


def next(self) -> int:
temp = self.stack.pop()
self.dfs(temp.right)
return temp.val


def hasNext(self) -> bool:
return len(self.stack) > 0



# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator(root)
# param_1 = obj.next()
# param_2 = obj.hasNext()
28 changes: 28 additions & 0 deletions DeleteNode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Time Complexity : O(1)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach : Copy the next node's value into the current and skip the next node.

'''
Your task is to delete the given node from
the linked list, without using head pointer.

Function Arguments: node (given node to be deleted)
Return Type: None, just delete the given node from the linked list.

{
# Node Class
class Node:
def __init__(self, data): # data -> value stored in node
self.data = data
self.next = None
}
'''
class Solution:
def deleteNode(self, del_node):
# code here
if del_node is None or del_node.next is None:
return
del_node.data = del_node.next.data
del_node.next = del_node.next.next
44 changes: 44 additions & 0 deletions Intersection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Time Complexity : O(m+n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach : Count the lengths of both lists.
# Move the longer list ahead to match the length.

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
currA, currB = headA, headB
countA, countB = 0,0

while currA:
currA = currA.next
countA += 1

while currB:
currB = currB.next
countB += 1

currA = headA
currB = headB

while countA > countB:
currA = currA.next
countA -= 1

while countB > countA:
currB = currB.next
countB -= 1

while currA != currB:
currA = currA.next
currB = currB.next

return currA


48 changes: 48 additions & 0 deletions Reorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Time Complexity : O(n)
# Space Complexity : O(1)
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# Approach : Find the middle of the lis.
# Reverse the second half of the list.
# Merge the two halves in a zig-zag manner.

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
if head is None or head.next is None:
return

slow, fast = head, head

while fast and fast.next:
slow = slow.next
fast = fast.next.next

prev, curr = None, slow.next
while curr:
temp = curr.next
curr.next = prev
prev = curr
curr = temp

slow.next = None

fast, slow = prev, head

while fast:
temp = slow.next
slow.next = fast
fast = fast.next
slow.next.next = temp
slow = temp