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
58 changes: 58 additions & 0 deletions BSTIterator.java
Original file line number Diff line number Diff line change
@@ -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<TreeNode> 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();
*/
30 changes: 30 additions & 0 deletions DeleteWithoutHeadPointer.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
58 changes: 58 additions & 0 deletions IntersectionofTwoLinkedLists.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
57 changes: 57 additions & 0 deletions ReorderList.java
Original file line number Diff line number Diff line change
@@ -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;
}
}