This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Given two (singly) linked lists, determine if the two lists intersect. Return the intersecting node. | |
Note that the intersection is defined based on reference not value. That is, if the kth node of the first | |
linked list is the exact same node (by reference) as the jth node of the second linked list, then they are | |
intersecting. | |
*/ | |
function getIntersection(h1: Node, h2: Node) { | |
const nodes1 = getNodesMap(h1) | |
const nodes2 = getNodesMap(h2) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isPalindrome(head: Node): boolean { | |
if (!head) return true | |
let str = [head.data] | |
let node = head | |
while(node.next) { | |
node = node.next | |
str.push(node.data) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
You have two numbers represented by a linked list, where each node contains a single | |
digit. The digits are stored in reverse order, such that the 1s digit is at the head of the list. Write a | |
function that adds the two numbers and returns the sum as a linked list. | |
input: (7 -> 1 -> 6) + (5 -> 9 -> 2). That is 617 + 295 | |
output: 2 -> 9 -> 1. That is 912 | |
Also write a solution for the forward order of digits |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"lastUpload":"2021-05-16T15:10:15.000Z","extensionVersion":"v3.4.3"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deleteSomeNode(node: Node) { | |
if (!node) { | |
return | |
} | |
if (!node.next) { | |
node = undefined | |
} | |
if (node.next) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function returnKthToLast(head: Node, k: number): Node | null { | |
let delta = 0 | |
let node = head | |
while (delta < k) { | |
node = node.next | |
++delta | |
if (!node) { | |
return null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function deleteDuplicates(head: Node): Node { | |
const occurredVals = new Map() | |
let node = head | |
occurredVals.set(node.data, true) | |
while (node.next != null) { | |
if (occurredVals.get(node.next.data) === true) { | |
node.next = node.next.next | |
} else { | |
occurredVals.set(node.next.data, true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If an element in an NxM matrix is zero, then every element in that row and column should be zero | |
function zeroOutMatrix(m: number[][]): number[][] { | |
const zeroedRows = new Map() | |
const zeroedCols = new Map() | |
for (let r = 0; r < m.length; ++r) { | |
for (let c = 0; c < m[r].length; c++){ | |
if (m[r][c] === 0) { | |
zeroedCols.set(c, true) | |
zeroedRows.set(r, true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Rotate a NxN matrix by 90 degrees in place where each pixel in the matrix is 4 bytes. | |
*/ | |
/* example call: | |
rotateMatrix(sanitizeData([ | |
[1,2,3], | |
[8,9,4], | |
[7,6,5] | |
])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Assume you have a method called isSubstring() which checks if one word is a subtring of another. | |
Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to | |
isSubstring(). Eg "waterbottle is a rotation of erbottlewat" | |
*/ | |
function checkSubstr(s1: string, s2: string): boolean { | |
if (s1.length !== s2.length) { | |
return false | |
} | |
const rotationByIndex = new Map() |