From f932e29dd0b15afaf2b591b48f2b334da40265c4 Mon Sep 17 00:00:00 2001 From: dewann Date: Sun, 22 Feb 2026 07:19:50 -0800 Subject: [PATCH 1/4] Implement BSTIterator for in-order traversal --- Problem1 | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Problem1 diff --git a/Problem1 b/Problem1 new file mode 100644 index 00000000..c6dafeeb --- /dev/null +++ b/Problem1 @@ -0,0 +1,51 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ + //TC On + //SC On +class BSTIterator { + TreeNode root; + Queue q = new LinkedList(); + + public BSTIterator(TreeNode root) { + this.root = root; + helper(root); + + } + + public int next() { + return q.poll(); + } + + public boolean hasNext() { + return !q.isEmpty(); + } + + private void helper(TreeNode root) { + if (root == null) + return ; + helper(root.left); + q.add(root.val); + helper(root.right); + + } +} + +/** + * Your BSTIterator object will be instantiated and called as such: + * BSTIterator obj = new BSTIterator(root); + * int param_1 = obj.next(); + * boolean param_2 = obj.hasNext(); + */ From 1541aa4affed707485d3e6013fa4dade8b24d5db Mon Sep 17 00:00:00 2001 From: dewann Date: Fri, 13 Mar 2026 14:56:45 -0700 Subject: [PATCH 2/4] Add reorderList method for linked list reordering Implement reorderList method to reorder a linked list in place. --- Problem2 | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Problem2 diff --git a/Problem2 b/Problem2 new file mode 100644 index 00000000..c6706290 --- /dev/null +++ b/Problem2 @@ -0,0 +1,57 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ + //TC On SC O1 +class Solution { + public void reorderList(ListNode head) { + ListNode slow = head; + ListNode fast = head; + //find mid + ListNode mid = mid(head); + // rev + ListNode revHead = revList(mid.next); + //map + mid.next=null; + fast=revHead; + while(fast!=null){ + ListNode temp=slow.next; + slow.next=fast; + fast=fast.next; + slow.next.next=temp; + slow=temp; + + } + + } + + private ListNode mid(ListNode head) { + ListNode slow = head; + ListNode fast = head; + while (fast != null && fast.next != null) { + slow = slow.next; + fast = fast.next.next; + } + return slow; + + } + + private ListNode revList(ListNode head) { + ListNode curr = head; + ListNode prev = null; + while (curr != null) { + ListNode temp = curr.next; + curr.next = prev; + prev = curr; + curr = temp; + } + return prev; + + } +} From 93591be988257f3e8879c77586ae05879708f937 Mon Sep 17 00:00:00 2001 From: dewann Date: Mon, 23 Mar 2026 22:32:50 -0700 Subject: [PATCH 3/4] Implement deleteNode method to remove a node --- Problem3 | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Problem3 diff --git a/Problem3 b/Problem3 new file mode 100644 index 00000000..2dd76832 --- /dev/null +++ b/Problem3 @@ -0,0 +1,25 @@ +//tc On +//sc o1 + +/* +class Node +{ + int data ; + Node next; + Node(int d) + { + data = d; + next = null; + } +} +*/ + +class Solution { + public void deleteNode(Node del_node) { + // code here + Node temp=del_node.next; + del_node.data=temp.data; + del_node.next=temp.next; + temp=null; + } +} From 869056ba0daa3d959d6ada3dc694d12364caae6b Mon Sep 17 00:00:00 2001 From: dewann Date: Mon, 23 Mar 2026 22:34:56 -0700 Subject: [PATCH 4/4] Add getIntersectionNode method for linked lists Implement method to find intersection of two linked lists. --- Problem4 | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Problem4 diff --git a/Problem4 b/Problem4 new file mode 100644 index 00000000..07334842 --- /dev/null +++ b/Problem4 @@ -0,0 +1,65 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ +// TC O(m+n) +SC O(1) +public class Solution { + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + + if (headA == headB) { + return headA; + } + + int countA = 0; + int countB = 0; + int skipSteps = -1; + boolean isAbig = false; + ListNode tempA=headA; + + ListNode tempB=headB; + while (headA.next != null) { + headA=headA.next; + countA++; + + } + while (headB.next != null) { + headB=headB.next; + countB++; + } + if (countA > countB) { + skipSteps = countA - countB; + isAbig = true; + } + if (countB > countA) { + skipSteps = countB - countA; + } + headA=tempA; + headB=tempB; + + if (isAbig && skipSteps != -1) { + for(int i=0;i