From d760d423f4b647adf89b915e5635e0cbcf560bfa Mon Sep 17 00:00:00 2001 From: Aahi Awasthee Date: Fri, 26 Dec 2025 23:46:47 -0600 Subject: [PATCH] Solved LL2 Problems --- bst-iterator.java | 32 +++++++++++++++++++++++++++ delete-without-head.java | 15 +++++++++++++ intersecrion-of-ll.java | 41 +++++++++++++++++++++++++++++++++++ reorder-list.java | 47 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+) create mode 100644 bst-iterator.java create mode 100644 delete-without-head.java create mode 100644 intersecrion-of-ll.java create mode 100644 reorder-list.java diff --git a/bst-iterator.java b/bst-iterator.java new file mode 100644 index 00000000..0d205b90 --- /dev/null +++ b/bst-iterator.java @@ -0,0 +1,32 @@ +// Time Complexity : O(n) +// Space Complexity : O(h) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : + + +// Your code here along with comments explaining your approach + +class BSTIterator { + Stack st; + public BSTIterator(TreeNode root) { + this.st = new Stack<>(); + dfs(root); + } + + private void dfs(TreeNode curr){ + while(curr !=null){ + st.add(curr); + curr = curr.left; + } + } + + public int next() { //amortized O(1) + TreeNode result = st.pop(); + dfs(result.right); //only add the next node when the prev node is removed. + return result.val; + } + + public boolean hasNext() { //O(1) + return !st.isEmpty(); + } +} diff --git a/delete-without-head.java b/delete-without-head.java new file mode 100644 index 00000000..6b69241f --- /dev/null +++ b/delete-without-head.java @@ -0,0 +1,15 @@ +// Time Complexity : O(1) +// Space Complexity :(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : + + +class Solution { + public void deleteNode(Node del_node) { + // code here + if(del_node == null || del_node.next == null) return; + + del_node.data = del_node.next.data; + del_node.next = del_node.next.next; + } +} \ No newline at end of file diff --git a/intersecrion-of-ll.java b/intersecrion-of-ll.java new file mode 100644 index 00000000..30014e32 --- /dev/null +++ b/intersecrion-of-ll.java @@ -0,0 +1,41 @@ +// Time Complexity : O(n) +// Space Complexity :(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : + + +// Your code here along with comments explaining your approach +// get the length of both arrays +// get tge difference and start the second pointer of the larger ll that many node ahead +//where both the pointer nodes are same that is the intersection + +public class Solution { + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + int lenA = 0; + ListNode curr = headA; + + while (curr != null) { + lenA++; + curr = curr.next; + } + int lenB = 0; + curr = headB; + while (curr != null) { + lenB++; + curr = curr.next; + } + while(lenA > lenB){ + headA = headA.next; + lenA--; + } + while(lenB > lenA){ + headB = headB.next; + lenB--; + } + while(headA != headB){ + headA = headA.next; + headB = headB.next; + } + return headA; + } +} \ No newline at end of file diff --git a/reorder-list.java b/reorder-list.java new file mode 100644 index 00000000..6f886d79 --- /dev/null +++ b/reorder-list.java @@ -0,0 +1,47 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : Yes +// Any problem you faced while coding this : + + +// Your code here along with comments explaining your approach +//Get the middle of the linkedlist +//reverse the second linkedlist +//merge the two + +class Solution { + public void reorderList(ListNode head) { + ListNode slow = head; + ListNode fast = head; + while (fast.next != null && fast.next.next != null) { + slow = slow.next; + fast = fast.next.next; + } + + fast = reverseList(slow.next); + slow.next = null; + slow = head; + + //merge + while (fast != null) { + ListNode temp = slow.next; + slow.next = fast; + fast = fast.next; + slow.next.next = temp; + slow = temp; + } + } + + private ListNode reverseList(ListNode head) { + ListNode prev = null; + ListNode curr = head; + + while (curr != null) { + ListNode temp = curr.next; + curr.next = prev; + prev = curr; + curr = temp; + } + return prev; + } +} \ No newline at end of file