From 3c3b6a831f2758e442435e88f713e0431d503cfe Mon Sep 17 00:00:00 2001 From: Shinjanee Gupta Date: Sat, 28 Feb 2026 01:18:24 -0800 Subject: [PATCH] LinkedList-2 --- BSTIterator.py | 39 +++++++++++++++++++++++++++++++++++++++ DeleteNode.py | 28 ++++++++++++++++++++++++++++ Intersection.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ Reorder.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+) create mode 100644 BSTIterator.py create mode 100644 DeleteNode.py create mode 100644 Intersection.py create mode 100644 Reorder.py diff --git a/BSTIterator.py b/BSTIterator.py new file mode 100644 index 00000000..69700b04 --- /dev/null +++ b/BSTIterator.py @@ -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() \ No newline at end of file diff --git a/DeleteNode.py b/DeleteNode.py new file mode 100644 index 00000000..a180b2f9 --- /dev/null +++ b/DeleteNode.py @@ -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 \ No newline at end of file diff --git a/Intersection.py b/Intersection.py new file mode 100644 index 00000000..ee0ba7a1 --- /dev/null +++ b/Intersection.py @@ -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 + + \ No newline at end of file diff --git a/Reorder.py b/Reorder.py new file mode 100644 index 00000000..7587414e --- /dev/null +++ b/Reorder.py @@ -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 + + + + \ No newline at end of file