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
63 changes: 63 additions & 0 deletions Intersection-of-linked-lists.java
Original file line number Diff line number Diff line change
@@ -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<m-n; i++) {
headA=headA.next;
}
} else {
for(int i=0; i<n-m; i++) {
headB=headB.next;
}
}

while(headA!=null) {
if(headA==headB) return headA;
headA=headA.next;
headB=headB.next;
}
return null;
}
}


// Solution -2


// TimeComplexity: O(m+n)
// SpaceComplexity: O(m+n)

public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
HashSet<ListNode> 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;

}
}
27 changes: 27 additions & 0 deletions binary-search-tree-operator .java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class BSTIterator {
Stack<TreeNode> 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();
}
}
35 changes: 35 additions & 0 deletions delete-without-head-pointer.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
72 changes: 72 additions & 0 deletions reorder-list.java
Original file line number Diff line number Diff line change
@@ -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<n) {
fast = fast.next;
i++;
}
curr.next = fast.next;
fast.next = null;
curr.next.next = nxt;
}
}
}