From a37460d2eb62fec205cfc0fd7bee4af1d03c24b9 Mon Sep 17 00:00:00 2001 From: Sarvani Baru Date: Sat, 28 Feb 2026 17:41:04 -0800 Subject: [PATCH] Done LinkedLists-2 --- BSTIterator.java | 58 +++++++++++++++++++++++++++++++ DeleteWithoutHeadPointer.java | 30 ++++++++++++++++ IntersectionofTwoLinkedLists.java | 58 +++++++++++++++++++++++++++++++ ReorderList.java | 57 ++++++++++++++++++++++++++++++ 4 files changed, 203 insertions(+) create mode 100644 BSTIterator.java create mode 100644 DeleteWithoutHeadPointer.java create mode 100644 IntersectionofTwoLinkedLists.java create mode 100644 ReorderList.java diff --git a/BSTIterator.java b/BSTIterator.java new file mode 100644 index 00000000..c7aaa128 --- /dev/null +++ b/BSTIterator.java @@ -0,0 +1,58 @@ +// Time Complexity : O(1) +// Space Complexity : O(n) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +Have a stack where we iteratively push the root and all its left children such that through LIFO policy, +we obtain the inorder. However, we need to make sure the right children also need to be pushed as part of +the traversal. So we push them while we are popping out the left children. This way, lazy evaluation is +maintained. + */ +/** + * 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; + * } + * } + */ +class BSTIterator { + Stack st; + public BSTIterator(TreeNode root) { + this.st = new Stack<>(); + helper(root); + } + + public int next() { + TreeNode temp = st.pop(); + helper(temp.right); + return temp.val; + } + + public boolean hasNext() { + return !st.isEmpty(); + } + + private void helper(TreeNode root) { + while(root != null) { + st.push(root); + root = root.left; + } + } +} + +/** + * 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(); + */ \ No newline at end of file diff --git a/DeleteWithoutHeadPointer.java b/DeleteWithoutHeadPointer.java new file mode 100644 index 00000000..74150fde --- /dev/null +++ b/DeleteWithoutHeadPointer.java @@ -0,0 +1,30 @@ +// Time Complexity : O(1) +// Space Complexity : O(1) +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +As we dont have head and cant access previous node, we will shift the next node's data backward and +delete the next node instead. We are just copying the data from the next node into current node and leaving +the removed node to garbage collector to clean it. + */ +/* +class Node +{ + int data ; + Node next; + Node(int d) + { + data = d; + next = null; + } +} +*/ + +class Solution { + public void deleteNode(Node del_node) { + // code here + del_node.data = del_node.next.data; + del_node.next = del_node.next.next; + } +} \ No newline at end of file diff --git a/IntersectionofTwoLinkedLists.java b/IntersectionofTwoLinkedLists.java new file mode 100644 index 00000000..6aee491a --- /dev/null +++ b/IntersectionofTwoLinkedLists.java @@ -0,0 +1,58 @@ +// 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 + +// Your code here along with comments explaining your approach +/* +Compute the sizes of both the linked lists by using count variables.The idea is to move the pointer by m - n times + of that list whose size is greater(if m > n). Else, n-m times(if n> m).This way, we can now start moving + the heads of both the linked lists one step at a time until they intersect. Even if non intersecting, + we return null in that case. + */ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ +public class Solution { + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + int countA = 0, countB = 0; + ListNode currA = headA; + ListNode currB = headB; + + while(currA != null) { + currA = currA.next; + countA++; + } + + while(currB != null) { + currB = currB.next; + countB++; + } + + currA = headA; + currB = headB; + while(countA > countB) { + currA = currA.next; + countA--; + } + + while(countB > countA) { + currB = currB.next; + countB--; + } + + while(currA != currB) { + currA = currA.next; + currB = currB.next; + } + return currA; + } +} \ No newline at end of file diff --git a/ReorderList.java b/ReorderList.java new file mode 100644 index 00000000..e0f75a8f --- /dev/null +++ b/ReorderList.java @@ -0,0 +1,57 @@ +// Time Complexity : O(n) +// Space Complexity : O(1) +// Did this code successfully run on Leetcode : yes +// Any problem you faced while coding this : no + +// Your code here along with comments explaining your approach +/* +The idea is to find the midpoint of the given list and reverse the second half of the list and then +connect the first part of the list to the second in an iterative way such that it meets the required way +of zigzag. Here the mid point can be computed by using 2 pointers where fast pointer moves ahead of slow +pointer and the position of slow pointer at which fast becomes null is the mid point. + */ +/** + * 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; } + * } + */ +class Solution { + public void reorderList(ListNode head) { + ListNode fast = head; + ListNode slow = head; + while(fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + } + ListNode revHead = helper(slow.next); + + slow.next = null; + slow = head; + fast = revHead; + + while(fast != null) { + ListNode temp = slow.next; + slow.next = fast; + fast = fast.next; + slow.next.next = temp; + slow = temp; + } + } + + private ListNode helper(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