From bc5bd24456271f8d0a3d8237ec2864835852bc78 Mon Sep 17 00:00:00 2001 From: subbu4061 Date: Thu, 5 Mar 2026 13:39:30 -0800 Subject: [PATCH] Adding all the solutions --- Intersection-of-linked-lists.java | 63 +++++++++++++++++++++++++++ binary-search-tree-operator .java | 27 ++++++++++++ delete-without-head-pointer.java | 35 +++++++++++++++ reorder-list.java | 72 +++++++++++++++++++++++++++++++ 4 files changed, 197 insertions(+) create mode 100644 Intersection-of-linked-lists.java create mode 100644 binary-search-tree-operator .java create mode 100644 delete-without-head-pointer.java create mode 100644 reorder-list.java diff --git a/Intersection-of-linked-lists.java b/Intersection-of-linked-lists.java new file mode 100644 index 00000000..4332326b --- /dev/null +++ b/Intersection-of-linked-lists.java @@ -0,0 +1,63 @@ +// Solution -1 + +// TimeComplexity: O(m+n) +// SpaceComplexity: O(1) +public class Solution { + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + ListNode first = headA; + ListNode second = headB; + int m =0; + int n =0; + while(first !=null) { + m++; + first = first.next; + } + while(second !=null) { + n++; + second = second.next; + } + if(m>n) { + for(int i=0; i set = new HashSet<>(); + while(headA!=null) { + set.add(headA); + headA= headA.next; + } + + while(headB!=null) { + if(set.contains(headB)) { + return headB; + } + set.add(headB); + headB= headB.next; + } + return null; + + } +} diff --git a/binary-search-tree-operator .java b/binary-search-tree-operator .java new file mode 100644 index 00000000..5a7d6ed4 --- /dev/null +++ b/binary-search-tree-operator .java @@ -0,0 +1,27 @@ +class BSTIterator { + Stack st; + public BSTIterator(TreeNode root) { + st = new Stack<>(); + dfs(root); + } + + private void dfs(TreeNode root) { + while(root!=null) { + st.add(root); + root = root.left; + } + } + + // TimeComplexity: O(h) but amortized O(1) + public int next() { + TreeNode curr = st.pop(); + dfs(curr.right); + return curr.val; + + } + + // TimeComplexity: O(1) + public boolean hasNext() { + return !st.isEmpty(); + } +} diff --git a/delete-without-head-pointer.java b/delete-without-head-pointer.java new file mode 100644 index 00000000..6da30797 --- /dev/null +++ b/delete-without-head-pointer.java @@ -0,0 +1,35 @@ +// Solution - 1 + +// Time Complexity: O(1) +// Space Complexity: O(1) + +class Solution { + public void deleteNode(Node del_node) { + // code here + Node curr = del_node; + Node temp = curr.next; + curr.data = curr.next.data; + curr.next = curr.next.next; + temp.next = null; + } +} + +// Solution - 2 + +// Time Complexity: O(n) +// Space Complexity: O(1) + +class Solution { + public void deleteNode(Node del_node) { + // code here + Node curr = del_node; + Node temp = curr.next; + while(temp.next!=null) { + curr.data = temp.data; + curr = temp; + temp = temp.next; + } + curr.data = temp.data; + curr.next = null; + } +} diff --git a/reorder-list.java b/reorder-list.java new file mode 100644 index 00000000..d8c8b731 --- /dev/null +++ b/reorder-list.java @@ -0,0 +1,72 @@ +// Solution -1 + +// TimeComplexity: O(n) +// SpaceComplexity: O(1) +class Solution { + public void reorderList(ListNode head) { + // 1. Find the middle + // 2. Reverse the second half + // 3. Merge both of them + ListNode slow = head; + ListNode fast = head; + + while(fast.next!=null && fast.next.next!=null) { + slow = slow.next; + fast = fast.next.next; + } + + ListNode curr = slow.next; + ListNode prev = null; + while(curr !=null) { + ListNode temp = curr.next; + curr.next = prev; + prev = curr; + curr = temp; + } + slow.next = null; + fast = prev; + slow = head; + + while(fast!=null){ + ListNode temp = slow.next; + slow.next = fast; + fast = fast.next; + slow.next.next = temp; + slow = temp; + } + } +} + + + +// Solution -2 + +// TimeComplexity: O(n^2) +// SpaceComplexity: O(1) + +class Solution { + public void reorderList(ListNode head) { + int n=0; + ListNode nxt = head; + while(nxt!=null) { + n++; + nxt = nxt.next; + } + nxt = head; + + while(nxt.next!=null && nxt.next.next !=null) { + ListNode curr = nxt; + ListNode fast = nxt; + nxt = nxt.next; + n = n-2; + int i =0; + while(i