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
37 changes: 37 additions & 0 deletions bst-iterator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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)
self.idx =0

def dfs(self, root):
if(root == None):
return
self.dfs(root.left)
self.stack.append(root.val)
self.dfs(root.right)


def next(self) -> int:
val =self.stack[self.idx]
self.idx+=1
return val



def hasNext(self) -> bool:
return True if self.idx < len(self.stack) else False



# Your BSTIterator object will be instantiated and called as such:
# obj = BSTIterator(root)
# param_1 = obj.next()
# param_2 = obj.hasNext()
14 changes: 14 additions & 0 deletions delete-node-without-head-pointer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution:
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
node.val = node.next.val
node.next = node.next.next
19 changes: 19 additions & 0 deletions intersection-of-ll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 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]:


l1 = headA
l2 = headB

while(l1!=l2):

l1=l1.next if l1 else headB
l2=l2.next if l2 else headA

return l2 or l1
49 changes: 49 additions & 0 deletions re-order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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.
"""
slow = head
fast = head
while(fast and fast.next):
slow = slow.next
fast = fast.next.next
second = slow.next
slow.next = None

reversedHead = self.reverseList(second)

fast = reversedHead
slow = head

while(fast):
temp_slow = slow.next
slow.next = fast
temp_fast = fast.next
fast.next = temp_slow
slow = temp_slow
fast = temp_fast


def reverseList(self, slow):


prev= None

while(slow):
temp =slow.next
slow.next = prev
prev = slow
slow = temp

return prev

while(prev!=None):
print(prev.val)
prev=prev.next