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
31 changes: 30 additions & 1 deletion Arrays/Sort an Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
Company Tags : Microsoft, Goldman Sachs, Cisco
Leetcode Link : https://leetcode.com/problems/sort-an-array/
*/

/**************************************** C++ ****************************************/
//solving using Counting Sort (O(n+k))
//S.C :O(k)
class Solution {
public:
vector<int> sortArray(vector<int>& nums) {
Expand All @@ -31,3 +32,31 @@ class Solution {
return nums;
}
};

/**************************************** JAVA ****************************************/
//solving using Counting Sort (O(n+k))
//S.C :O(k)
class Solution {
public int[] sortArray(int[] nums) {
int minE = Arrays.stream(nums).min().getAsInt();
int maxE = Arrays.stream(nums).max().getAsInt();

Map<Integer, Integer> mp = new HashMap<>();

for (int num : nums) {
mp.put(num, mp.getOrDefault(num, 0) + 1);
}

int i = 0;

for (int num = minE; num <= maxE; num++) {
while (mp.getOrDefault(num, 0) > 0) {
nums[i] = num;
i++;
mp.put(num, mp.get(num) - 1);
}
}

return nums;
}
}
116 changes: 116 additions & 0 deletions Linked List/Reverse Linked List II.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/


/**************************************** C++ ****************************************/
//Approach-1
class Solution {
public:
Expand Down Expand Up @@ -135,3 +136,118 @@ class Solution {
return dummy->next;
}
};


/**************************************** JAVA ****************************************/
//Approach-1 Iterative Approach
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
if (head == null || head.next == null) {
return head;
}

ListNode dummy = new ListNode(0);
dummy.next = head;

ListNode prev = dummy;

for (int i = 1; i < left; i++) {
prev = prev.next;
}

ListNode curr = prev.next;

for (int i = 1; i <= right - left; i++) {
ListNode temp = prev.next;
prev.next = curr.next;
curr.next = curr.next.next;
prev.next.next = temp;
}

return dummy.next;
}
}

//Approach-2 Recursive Approach
class Solution {
public ListNode reverse(ListNode head, ListNode rightNode) {
if (head == null || head.next == null || head == rightNode) {
return head;
}

ListNode last = reverse(head.next, rightNode);
head.next.next = head;
head.next = null;
return last;
}

public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode leftNode = null;
ListNode leftPrev = null;
ListNode rightNode = null;
ListNode rightNext = null;

ListNode curr = head;
int c = 1;
while (c < left) {
leftPrev = curr;
curr = curr.next;
c++;
}
leftNode = curr;

while (c < right) {
curr = curr.next;
c++;
}

rightNode = curr;
rightNext = rightNode.next;

ListNode temp = reverse(leftNode, rightNode);

if (leftPrev != null && leftPrev.next != null) {
leftPrev.next.next = rightNext;
leftPrev.next = temp;
} else {
head.next = rightNext;
head = temp;
}

return head;
}
}

//Approach-3 Using stack
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
if (head == null || head.next == null) {
return head;
}

Stack<ListNode> stack = new Stack<>();
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode prev = dummy;

for (int i = 1; i <= left - 1; i++) {
prev = prev.next;
}

ListNode curr = prev.next;
for (int i = left; i <= right; i++) {
stack.push(curr);
curr = curr.next;
}

ListNode storeRightNext = stack.peek().next;

while (!stack.isEmpty()) {
prev.next = stack.pop();
prev = prev.next;
}

prev.next = storeRightNext;
return dummy.next;
}
}