Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Competitive Coding 3 completed #951

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
21 changes: 21 additions & 0 deletions Balanced Binary Tree Question
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
110. Balanced Binary Tree {Easy}

Given a binary tree, determine if it is
height-balanced.

Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: true

Example 2:
Input: root = [1,2,2,3,3,null,null,4,4]
Output: false

Example 3:
Input: root = []
Output: true

Constraints:

The number of nodes in the tree is in the range [0, 5000].
-104 <= Node.val <= 104
52 changes: 52 additions & 0 deletions BalancedBinaryTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Time Complexity:
The method height is a recursive function that visits each node once.
At each node:
It recursively computes the height of the left and right subtrees.
It performs constant-time checks (e.g., if conditions, Math.abs, and Math.max).
Thus, the total time complexity is O(n), where n is the number of nodes in the tree.

Space Complexity:
The space complexity is O(h), where h is the height of the tree:
Worst Case (Skewed Tree): O(n)
Best Case (Balanced Tree): O(logn)

Did this code successfully run on Leetcode: Yes
*/

//Definition for a binary tree node.
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 BalancedBinaryTree {
public boolean isBalanced(TreeNode root)
{
if(root == null) return true;
return height(root) != -1;
}

private int height(TreeNode root)
{
//base
if(root == null) return 0;

//logic
int left = height(root.left);
int right = height(root.right);

if(left == -1 || right == -1) return -1;
if(Math.abs(left-right) > 1) return -1;

return Math.max(left, right) + 1;
}
}
22 changes: 22 additions & 0 deletions Palindrome Linked List Question
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
234. Palindrome Linked List {Easy}

Given the head of a singly linked list, return true if it is a palindrome or false otherwise.

Example 1:

Input: head = [1,2,2,1]
Output: true

Example 2:

Input: head = [1,2]
Output: false


Constraints:

The number of nodes in the list is in the range [1, 105].
0 <= Node.val <= 9


Follow up: Could you do it in O(n) time and O(1) space?
85 changes: 85 additions & 0 deletions PalindromeLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Time complexity : O(n), where n is the number of nodes in the Linked List.
Finding the middle is O(n), reversing a list in place is O(n), and then comparing the 2 resulting Linked Lists
is also O(n) => O(n) + O(n) + O(n) = O(3n) = O(n)

Space complexity : O(1) as we are not using any auxiliary data structure

Did this code successfully run on Leetcode: Yes
*/

/**
* 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 PalindromeLinkedList {
public boolean isPalindrome(ListNode head)
{
//base case
if(head == null) return true;

//logic
ListNode slow = head;
ListNode fast = head;

//Finding the mid of the list
while(fast.next != null && fast.next.next != null)
{
slow = slow.next;
fast = fast.next.next;
}

//Now we will be having the mid point at slow
ListNode head2 = slow.next; //slow.next here is the midpoint
slow.next = null;

/*
1 -> 2 -> 2 -> 3 -> 2 -> 2 -> 1 -> null (3 is midpoint which is also slow)

After finding the midpoint
left sublist = 1 -> 2 -> 2 -> 3 -> null (3 is head2)
right sublist = 2 -> 2 -> 1 -> null
*/

/*
Now we need to reverse the right sublist to compare it with left sublist, make head2 point to the reversed list first node
meaning now it is pointing 2 make it point to 1
*/
head2 = reverseList(head2);

//After getting the reverse list, check whether elements in both the list are equal
while(head != null && head2 != null)
{
if(head.val != head2.val) {
return false;
}

head = head.next;
head2 = head2.next;
}

return true;
}

private ListNode reverseList(ListNode head)
{
ListNode prev = null;
ListNode curr = head;

while(curr != null)
{
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}

return prev;
}
}
26 changes: 26 additions & 0 deletions Reverse Linked List Question
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
https://leetcode.com/problems/reverse-linked-list/description/

206. Reverse Linked List {Easy}

Given the head of a singly linked list, reverse the list, and return the reversed list.

Example 1:

Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
Example 2:

Input: head = [1,2]
Output: [2,1]
Example 3:

Input: head = []
Output: []

Constraints:

The number of nodes in the list is the range [0, 5000].
-5000 <= Node.val <= 5000


Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?
34 changes: 34 additions & 0 deletions ReverseLinkedListIterative.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Time complexity : O(n). List of n nodes, traverse over each node once

Space complexity : O(1).

Did this code successfully run on Leetcode: Yes
*/

//Definition for singly-linked list.
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 ReverseLinkedListIterative {
public ListNode reverseList(ListNode head)
{
ListNode prev = null;
ListNode curr = head;

while(curr != null)
{
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}

return prev;
}
}
34 changes: 34 additions & 0 deletions ReverseLinkedListRecursive.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Time complexity : O(n). List of n nodes, traverse over each node once

Space complexity : O(n)
The extra space comes from implicit stack space due to recursion. The recursion could go up to n levels deep. Means,
so list of length n we make n recursive calls.

Did this code successfully run on Leetcode: Yes
*/

/**
* 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 ReverseLinkedListRecursive {
public ListNode reverseList(ListNode head)
{
//base case
if( head == null || head.next == null) return head;

//logic
ListNode reverseSubListHead = reverseList(head.next);
head.next.next = head;
head.next = null;

return reverseSubListHead;
}
}