Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions bst-iterator.java
Original file line number Diff line number Diff line change
@@ -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<TreeNode> 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();
}
}
15 changes: 15 additions & 0 deletions delete-without-head.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
41 changes: 41 additions & 0 deletions intersecrion-of-ll.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
47 changes: 47 additions & 0 deletions reorder-list.java
Original file line number Diff line number Diff line change
@@ -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;
}
}