Skip to content

Instantly share code, notes, and snippets.

View AlexeiDarmin's full-sized avatar
👋
software developer and simulation tinkerer

Alexei Darmin AlexeiDarmin

👋
software developer and simulation tinkerer
View GitHub Profile
/*
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)
@AlexeiDarmin
AlexeiDarmin / isPalindromeLinkedList.ts
Created November 15, 2018 03:58
Is this linked list a palindrome
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)
}
@AlexeiDarmin
AlexeiDarmin / reverseAndForwardSum.ts
Created November 15, 2018 03:38
Write a function that adds the two numbers represented as linked lists and returns the sum as a linked list
/*
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
@AlexeiDarmin
AlexeiDarmin / cloudSettings
Last active May 16, 2021 15:10
Linked list partition
{"lastUpload":"2021-05-16T15:10:15.000Z","extensionVersion":"v3.4.3"}
@AlexeiDarmin
AlexeiDarmin / deleteSomeNode.ts
Created November 12, 2018 00:24
Delete a middle node from a singly-linked list
function deleteSomeNode(node: Node) {
if (!node) {
return
}
if (!node.next) {
node = undefined
}
if (node.next) {
@AlexeiDarmin
AlexeiDarmin / kthToLastNode.ts
Created November 12, 2018 00:15
return the kth to last node in a linked list
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
@AlexeiDarmin
AlexeiDarmin / removeDuplicates.ts
Created November 11, 2018 23:47
Remove duplicates from a linked list
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)
@AlexeiDarmin
AlexeiDarmin / zeroOutMatrix.ts
Created November 11, 2018 23:14
If an element in an NxM matrix is zero, then every element in that row and column should be zero
// 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)
@AlexeiDarmin
AlexeiDarmin / rotateBitMatrix.ts
Created November 11, 2018 22:31
Rotate a NxN matrix by 90 degrees in place where each pixel in the matrix is 4 bytes.
/*
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]
]))
@AlexeiDarmin
AlexeiDarmin / checkSubstrRotation.ts
Created November 11, 2018 17:58
Given two strings, s1 and s2, write code to check if s2 is a rotation of s1 using only one call to isSubstring().
/*
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()