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
| 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; | |
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
| const reverseList = function(head) { | |
| let previousNode = null; | |
| let currentNode = head; | |
| let nextNode = null; | |
| while(currentNode) { | |
| //reverse the pointers | |
| nextNode = currentNode.next; | |
| currentNode.next = previousNode; |
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
| 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 |
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
| 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); |
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
| 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; |
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
| 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) { |
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
| 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; |
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
| 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; |
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
| 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]; | |
| } | |
| } |
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
| 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){ |