Recursion
Easy
Write a function that reverses a string. The input string is given as an array of characters s.
You must do this by modifying the input array in-place with O(1) extra memory.~
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Input: s = ["H","a","n","n","a","h"]
Output: ["h","a","n","n","a","H"]
// _(┐ ◟;゚д゚)ノ
// Time O(n) | Space O(1); n is the input length
var reverseString = function(s) {
let leftPointer = 0;
let rightPointer = s.length - 1;
while(leftPointer < rightPointer) {
const oldLeft = s[leftPointer];
const oldRight = s[rightPointer];
s[leftPointer] = oldRight;
s[rightPointer] = oldLeft;
leftPointer ++;
rightPointer --;
}
return s;
};
/**
* time: O(n) - where n is the length of the string
* space: O(n) - where n is the length of the string
*/
var reverseString = function(s) {
const reverse = (left, right) => {
if(left > right) return s;
[s[left], s[right]] = [s[right], s[left]];
return reverse(left + 1, right - 1);
}
reverse(0, s.length - 1);
};
/**
* Time complexity would be O(n) which n is arr.length - 1.
* Space complexity would be O(n) which n is arr.length - 1.
*/
var reverseString = function(s) {
const reverseStringInner = (arr, idx) => {
const { length } = arr;
const isOdd = length % 2 === 1;
if (isOdd && idx === ((length - 1) / 2)) return arr;
else if (idx === length / 2) return arr;
const leftIdx = idx;
const leftValue = arr[leftIdx];
const rightIdx = (arr.length - 1) - idx;
const rightValue = arr[rightIdx];
arr[leftIdx] = rightValue;
arr[rightIdx] = leftValue;
return reverseStringInner(arr, idx + 1);
};
return reverseStringInner(s, 0);
};
// Time O(n) Space O(n)
var reverseString = function(s, index = 0) {
const len = s.length, lastIndex = len - 1;
[s[index], s[lastIndex - index]] = [s[lastIndex - index], s[index]];
if(index + 1 < len / 2){
return reverseString(s, index + 1);
}
return s
};
You are given the heads of two sorted linked lists list1 and list2.
Feb 2, 2025You're given two Linked Lists of potentially unequal length. These Linked Lists potentially merge at a shared intersection node. Write a function that returns the intersection node or returns None / null if there is no intersection.Each LinkedList node has an integer value as well as a next node pointing to the next node in the list or to None null if it's the tail of the list.Note: Your function should return an existing node. It should not modify either Linked List, and it should not create any new Linked Lists.
Feb 2, 2025Write a function that takes in the head of a Singly Linked List and an integer k and removes the kth node from the end of the list.The removal should be done in place, meaning that the original data structure should be mutated (no new structure should be created).Furthermore, the input head of the linked list should remain the head of the linked list after the removal is done, even if the head is the node that's supposed to be removed. In other words, if the head is the node that's supposed to be removed, your function should simply mutate its value and next pointer.Note that your function doesn't need to return anything.You can assume that the input Linked List will always have at least two nodes and, more specifically, at least k nodes.Each LinkedList node has an integer value as well as a next node pointing to the next node in the list or to None / null if it's the tail of the list.
Feb 2, 2025https://leetcode.com/problems/best-time-to-buy-and-sell-stock/solutions/1735550/python-javascript-easy-solution-with-very-clear-explanation/
Aug 27, 2023or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up