Skip to content

Instantly share code, notes, and snippets.

@lienista
lienista / 92-leetcode-reverse-linked-list-ii.js
Last active August 28, 2018 19:05
(Algorithms in Javascript) Leetcode 92. Reverse Linked List II - Reverse a linked list from position m to n. Do it in one-pass.
const reverseBetween = (head, m, n) => {
if(m>=n) return head;
let currentNode = head;
let previousNode = head;
let temp = null, temp2 = null, tempStart = null;
let countm = 0, countn=0;
if(!currentNode.next) return head;
let startNode = head;
@lienista
lienista / leetcode-206-reverse-a-linked-list.js
Last active October 11, 2018 22:52
(Algorithms in Javascript) Leetcode 206. Reverse Linked List - Reverse a singly linked list.
const reverseList = function(head) {
let previousNode = null;
let currentNode = head;
let nextNode = null;
while(currentNode) {
//reverse the pointers
nextNode = currentNode.next;
currentNode.next = previousNode;
@lienista
lienista / leetcode-2-add-two-integers-from-linked-lists.js
Last active October 11, 2018 22:52
Leetcode 2. Add Two Numbers - You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself.
const addTwoNumbers = (l1, l2) => {
if(l1 === null && l2 === null) {
return null;
}
if(l1 === null) return l2;
if(l2 === null) return l1;
let n1, n2, n, carry=0;
let l = new ListNode(); //our result
let pl = l; //pointer to run through l
@lienista
lienista / leetcode-2-sum-linked-lists.js
Last active October 11, 2018 22:53
Leetcode 2. Add Two Numbers - You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself.
const addTwoLists = (l1, l2) => {
let n1=0, n2=0, count=0;
while(l1 !== null) {
n1 += (l1.val)*Math.pow(10,count);
l1 = l1.next;
count++;
}
count=0;
while(l2 !== null) {
n2 += (l2.val)*Math.pow(10,count);
@lienista
lienista / leetcode-86-partition-linked-list.js
Last active October 11, 2018 22:53
Leetcode 86. Partition List - Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions.
const partition = (head, x) => {
if(head === null) return head;
let beforeStart = null,
beforeEnd = null,
afterStart = null,
afterEnd = null;
while(head) {
let next = head.next;
head.next = null;
@lienista
lienista / ctci-2.3-delete-middle-node.js
Last active August 28, 2018 18:54
(Algorithms in Javascript) CTCI 2.3 - Delete Middle Node: Implement an algorithm to delete a node in the middle (i.e., any node but the first and last node, not necessarily the exact middle) of a singly linked list, given only access to that node.
const removeElements = (head, val) => {
if(head === null) return head;
let current = head;
while(current.next !== null && current.val === val) {
current = current.next;
}
if(!current.next && current.val === val) return [];
head = current;
while(current.next !== null) {
if(current.next.val === val) {
@lienista
lienista / ctci-2.2-find-the-kth-to-last-element-of-a-singly-linked-list.js
Last active August 28, 2018 18:56
(Algorithms in Javascript) CTCI 2.2 - Return kth to last - Implement an algorithm to find the kth to last element of a singly linked list
const findKthFromLast = (head, n) => {
if(!head) return head;
if(!head.next && n>0) return [];
let p = head;
let pk = head;
//Move p k elements into the list
for(let i=0; i<n; i++){
p = p.next;
@lienista
lienista / leetcode-83-remove-duplicates-from-unsorted-list.js
Last active October 24, 2022 03:05
(Algorithms in Javascript) Leetcode 83. Remove Duplicates from Sorted List - Given a sorted linked list, delete all duplicates such that each element appear only once.
const removeDuplicates = (head) => {
let current = head;
let numList = {};
numList[current.val] = 1;
while (current != null && current.next != null) {
if (numList[current.next.val]) {
current.next = current.next.next;
} else {
current = current.next;
numList[current.val] = 1;
@lienista
lienista / leetcode-49-group-anagrams.js
Last active October 11, 2018 22:53
(Algorithms in Javascript) Leetcode 49. Group Anagrams - Given an array of strings, group anagrams together.
const groupAnagrams => (strs) => {
let charMap = {};
let anagram = (element, index) => {
let thisKey = element.split('').sort().join('');
if(charMap.hasOwnProperty(thisKey)){
charMap[thisKey].unshift(element);
} else {
charMap[thisKey] = [element];
}
}
@lienista
lienista / leetcode-16-closest-sum-of-3.js
Last active October 11, 2018 22:54
(Algorithms in Javascript) Leetcode 16. 3Sum Closest - Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
const threeSumClosest = (nums, target) => {
let len = nums.length, sum=0;
if(len === 0) return 0;
if(len <= 3) {
for(let i=0; i<len; i++){
sum += nums[i];
}
return sum;
}
nums.sort(function(a,b){