From 957568472ab57244ef1a5e25cc545b66d2065c86 Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Tue, 3 Mar 2026 14:48:35 -0800 Subject: [PATCH 1/2] Added Problem2 and Problem4 --- Problem2.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Problem4.py | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 105 insertions(+) create mode 100644 Problem2.py create mode 100644 Problem4.py diff --git a/Problem2.py b/Problem2.py new file mode 100644 index 00000000..709dfccd --- /dev/null +++ b/Problem2.py @@ -0,0 +1,62 @@ +# https://leetcode.com/problems/reorder-list/description/ + +# Time Complexity: O(n) +# Space Complexity: O(1) + +# This problem is about reordering a linked list. We are given a single linked list. The reordering looks like a zigzag pattern +# first node points to last node, then (last-1)th node points to second node and so on. We can implement this by using slow and fast pointers +# First get the middle pointer using slow and fast pointer. When fast reaches the lastnode (fast.next == None), then the wherever the slow points +# to becomes the middle node. The second half of the linked list can be reversed. Write a reverse linkedlist function and use it to reverse the second +# half. Now we can use slow and fast pointers to move in a zigzag pattern to return the resultant list. + +# Example: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> None +# Mid becomes 4, second half of the array is 5 -> 6 -> 7 +# After reversing the second half. +# 1 -> 2 -> 3 -> 4 +# 7 -> 6 -> 5 +# Reordered list = 1 -> 7 -> 2 -> 6 -> 3 -> 5 -> 4 + +# 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. + """ + + fast = head + slow = head + + # get the middle + while fast is not None and fast.next is not None: + fast = fast.next.next + slow = slow.next + + fast = self.reverse(slow.next) + slow.next = None + + slow = head + + while slow is not None and fast is not None: + temp = slow.next + slow.next = fast + temp1 = fast.next + fast.next = temp + slow = temp + fast = temp1 + + + def reverse(self, head): + prev = None + curr = head + + while curr is not None: + temp = curr.next + curr.next = prev + prev = curr + curr = temp + + return prev \ No newline at end of file diff --git a/Problem4.py b/Problem4.py new file mode 100644 index 00000000..01baa8bb --- /dev/null +++ b/Problem4.py @@ -0,0 +1,43 @@ +# https://leetcode.com/problems/intersection-of-two-linked-lists/ + +# + +# 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 = headA + currB = headB + + countA, countB = 0, 0 + + while currA is not None: + currA = currA.next + countA = countA + 1 + + while currB is not None: + currB = currB.next + countB = countB + 1 + + currA = headA + currB = headB + + while countA > countB: + currA = currA.next + countA = countA - 1 + + while countB > countA: + currB = currB.next + countB = countB - 1 + + while currA != currB: + currA = currA.next + currB = currB.next + + return currA + + From 928ecf88022694df9d53a12dd1153fae203de889 Mon Sep 17 00:00:00 2001 From: Megha Raykar Date: Wed, 4 Mar 2026 12:28:30 -0800 Subject: [PATCH 2/2] Problem1 added --- Problem1.py | 35 +++++++++++++++++++++++++++++++++++ Problem2.py | 3 +-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 Problem1.py diff --git a/Problem1.py b/Problem1.py new file mode 100644 index 00000000..0fbba9f4 --- /dev/null +++ b/Problem1.py @@ -0,0 +1,35 @@ +# https://leetcode.com/problems/binary-search-tree-iterator/ + +# Time Complexity: O(n) +# Space Complexity: O(h) + +# 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.s = deque() + self.helper(root) + + def helper(self, root): + while root is not None: + self.s.append(root) + root = root.left + + def next(self) -> int: + temp = self.s.pop() + self.helper(temp.right) + return temp.val + + def hasNext(self) -> bool: + return len(self.s) > 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/Problem2.py b/Problem2.py index 709dfccd..9025cac8 100644 --- a/Problem2.py +++ b/Problem2.py @@ -47,8 +47,7 @@ def reorderList(self, head: Optional[ListNode]) -> None: fast.next = temp slow = temp fast = temp1 - - + def reverse(self, head): prev = None curr = head