LeetCode 92. Reverse Linked List II

I copied solution for Reverse Linked List II from this Udemy course. It was a little hard to understand all these pointers in second loop, I had to use pen and paper to draw each step to finally understand how each pointer works. /** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {ListNode} head * @param {number} left * @param {number} right * @return {ListNode} */ var reverseBetween = function(head, left, right) { let currPos = 1; let currNode = head; let startNode = head; let endNode = null; while(currPos < left) { startNode = currNode; currNode = currNode.

Published on


LeetCode 206. Reverse Linked List

Here is a quick solution to Reverse Linked List problem. /** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {ListNode} head * @return {ListNode} */ var reverseList = function(head) { let curr = head; let prev = null; while (curr) { let next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; };

Published on


Importing plotly failed. Interactive plots will not work.

Fix: pip3 install --upgrade plotly

Published on


LeetCode 560. Subarray Sum Equals K

Brute Force Solution to Subarray Sum Equals K problem from LeetCode: /** * @param {number[]} nums * @param {number} k * @return {number} */ var subarraySum = function(nums, k) { let ans = 0; for (let i=0; i<nums.length; i++) { let currSum = nums[i]; if (currSum === k) ans++; for (let j=i+1; j<nums.length; j++) { currSum += nums[j]; if (currSum === k) ans++; } } return ans; }; Optimized solution after cheating a bit:

Published on


LeetCode 2. Add Two Numbers

Here is my solution to Add Two Numbers problem in PHP: /** * Definition for a singly-linked list. * class ListNode { * public $val = 0; * public $next = null; * function __construct($val = 0, $next = null) { * $this->val = $val; * $this->next = $next; * } * } */ class Solution { /** * @param ListNode $l1 * @param ListNode $l2 * @return ListNode */ function addTwoNumbers($l1, $l2) { $ans = new ListNode(0, null); $p = $l1; $q = $l2; $current = $ans; $carry=0; while ($p !

Published on


Docker in 5 minutes

I have been using Docker and Kubernetes for several years now but never really took any time to really read about it. There were a lot of gaps in my understanding of how Docker works and what really is possible with it. Just recently my employer started to offer Udemy’s subscription, so I took Hands on Docker course. Here are some of the important things I learned in this course.

Published on


LeetCode 7. Reverse Integer

My initial solution for Reverse Integer problem: /** * @param {number} x * @return {number} */ var reverse = function(x) { let ans = ''; if (x<0) { ans = '-'; x*=-1; } str = x.toString(); for (let i=str.length-1; i>=0; i--) { ans += str[i]; } ans = parseInt(ans); if (ans>2**31-1 || ans<(-2)**31) { return 0; } return ans; }; Results Runtime: 104 ms, faster than 34.78% of JavaScript online submissions for Reverse Integer.

Published on


LeetCode 3. Longest Substring Without Repeating Characters

Here is my solution in PHP to this problem. class Solution { /** * @param String $s * @return Integer */ function lengthOfLongestSubstring($s) { $n = strlen($s); $ans = 0; $i = 0; $j = 0; $arr = []; while ($i < $n && $j < $n) { if (!in_array($s[$j], $arr)) { $arr[] = $s[$j++]; $ans = max($ans, $j-$i); } else { array_splice($arr, 0, 1); $i++; } } return $ans; } } And here is solution JavaScript based on this Udemy lesson:

Published on


Json_error_ctrl_char

I was using mcrypt to encrypt json_encoded data. On other side, mycrypt was able to decrpyt data but json_decode was not working. It would throw JSON\_ERROR\_CTRL_CHAR error. However, without mcrypt, json_decode worked flawlessly. After some trial and error, I found that applying trim function after decrypting data would let me use json_decode without any issues.

Published on


Find Large Directories on AIX

I usually run it from under a partition which is running out of space. It can take a while. du -g | sort -n -r

Published on


Prev Next