Skip to content

Commit

Permalink
Updated 8 solutions.
Browse files Browse the repository at this point in the history
  • Loading branch information
RodneyShag committed Mar 22, 2019
1 parent 60d84d6 commit f0deb45
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 21 deletions.
1 change: 1 addition & 0 deletions Algorithms/Bit Manipulation/Sansa and XOR/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// in the original array to get our final answer.

// Time Complexity: O(n)
// Space Complexity: O(1)

static int sansaXor(int[] array) {
if (array.length % 2 == 0) { // Case 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
// Github: github.com/RodneyShag
// HackerRank: hackerrank.com/RodneyShag

// Algorithm
//
// 1. Create a pointer that moves 1 step at a time: 'slow'
// 2. Create a pointer that moves 2 steps at a time: 'fast'
// 3. If the Linked List has a cycle, 'slow' and 'fast' will collide.

/*
Detect a cycle in a linked list. Note that the head pointer may be 'null' if the list is empty.
A Node is defined as:
class Node {
Expand All @@ -30,3 +35,6 @@ boolean hasCycle(Node head) {
}
return false;
}

// Time Complexity: O(n)
// Space Complexity: O(1)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

// ArrayDeque is "likely to be faster than Stack when used as a stack" - Java documentation

// Time Complexity: O(n)
// Space Complexity: O(n)

/* Create HashMap to match opening brackets with closing brackets */
static String isBalanced(String expression) {
HashMap<Character, Character> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Github: github.com/RodneyShag
// HackerRank: hackerrank.com/RodneyShag

/* Hidden stub code will pass a root argument to the function below.
/*
The Node class is defined as follows:
class Node {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,24 @@
// HackerRank: hackerrank.com/RodneyShag

/*
Node is defined as
class Node {
class SinglyLinkedListNode {
int data;
Node next;
SinglyLinkedListNode next;
}
*/

// Time Complexity: O(n + m)
// Space Complexity: O(1)

Node MergeLists(Node currA, Node currB) {
static SinglyLinkedListNode mergeLists(SinglyLinkedListNode currA, SinglyLinkedListNode currB) {
if (currA == null) {
return currB;
} else if (currB == null) {
return currA;
}

/* Find new head pointer */
Node head = null;
if (currA.data < currB.data) {
head = currA;
currA = currA.next;
} else {
head = currB;
currB = currB.next;
}

/* Build rest of list */
Node n = head;
SinglyLinkedListNode result = new SinglyLinkedListNode(0); // dummy/placeholder ListNode
SinglyLinkedListNode n = result;
while (currA != null && currB != null) {
if (currA.data < currB.data) {
n.next = currA;
Expand All @@ -50,5 +39,5 @@ Node MergeLists(Node currA, Node currB) {
n.next = currA;
}

return head;
return result.next;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// Time Complexity: O(n)
// Space Complexity: O(1)
DoublyLinkedListNode reverse(DoublyLinkedListNode head) {
if (head == null && head.next == null) {
if (head == null || head.next == null) {
return head;
}
DoublyLinkedListNode prev = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

static SinglyLinkedListNode reverse(SinglyLinkedListNode head) {
if (head == null && head.next == null) {
if (head == null || head.next == null) {
return head;
}
SinglyLinkedListNode prev = null;
Expand Down
3 changes: 3 additions & 0 deletions Data Structures/Stacks/Balanced Brackets/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

// ArrayDeque is "likely to be faster than Stack when used as a stack" - Java documentation

// Time Complexity: O(n)
// Space Complexity: O(n)

public class Solution {
public static void main(String[] args) {
/* Create HashMap to match opening brackets with closing brackets */
Expand Down

0 comments on commit f0deb45

Please sign in to comment.