From b852482f79b52d578582bec2dbb08f4da32cf9e8 Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Sat, 1 May 2021 16:29:44 +0800 Subject: [PATCH 1/4] feat: add javascript solution to lcci problem: No.02.01.Remove Duplicate Node --- lcci/02.01.Remove Duplicate Node/README.md | 32 +++++++++++++++++++ lcci/02.01.Remove Duplicate Node/README_EN.md | 32 +++++++++++++++++++ lcci/02.01.Remove Duplicate Node/Solution.js | 27 ++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 lcci/02.01.Remove Duplicate Node/Solution.js diff --git a/lcci/02.01.Remove Duplicate Node/README.md b/lcci/02.01.Remove Duplicate Node/README.md index 159a271a25dd2..5a3a60855184e 100644 --- a/lcci/02.01.Remove Duplicate Node/README.md +++ b/lcci/02.01.Remove Duplicate Node/README.md @@ -102,6 +102,38 @@ class Solution { } ``` +### **JavaScript** + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ + var removeDuplicateNodes = function(head) { + if (head == null || head.next == null) return head; + const cache = new Set([]); + cache.add(head.val); + let cur = head, fast = head.next; + while (fast !== null) { + if (!cache.has(fast.val)) { + cur.next = fast; + cur = cur.next; + cache.add(fast.val); + } + fast = fast.next; + } + cur.next = null; + return head; +}; +``` + ### **...** ``` diff --git a/lcci/02.01.Remove Duplicate Node/README_EN.md b/lcci/02.01.Remove Duplicate Node/README_EN.md index 1e86e25aa4ce8..d618886cf6087 100644 --- a/lcci/02.01.Remove Duplicate Node/README_EN.md +++ b/lcci/02.01.Remove Duplicate Node/README_EN.md @@ -103,6 +103,38 @@ class Solution { } ``` +### **JavaScript** + +```javascript +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ + var removeDuplicateNodes = function(head) { + if (head == null || head.next == null) return head; + const cache = new Set([]); + cache.add(head.val); + let cur = head, fast = head.next; + while (fast !== null) { + if (!cache.has(fast.val)) { + cur.next = fast; + cur = cur.next; + cache.add(fast.val); + } + fast = fast.next; + } + cur.next = null; + return head; +}; +``` + ### **...** ``` diff --git a/lcci/02.01.Remove Duplicate Node/Solution.js b/lcci/02.01.Remove Duplicate Node/Solution.js new file mode 100644 index 0000000000000..294718cf23288 --- /dev/null +++ b/lcci/02.01.Remove Duplicate Node/Solution.js @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ + var removeDuplicateNodes = function(head) { + if (head == null || head.next == null) return head; + const cache = new Set([]); + cache.add(head.val); + let cur = head, fast = head.next; + while (fast !== null) { + if (!cache.has(fast.val)) { + cur.next = fast; + cur = cur.next; + cache.add(fast.val); + } + fast = fast.next; + } + cur.next = null; + return head; +}; \ No newline at end of file From 98053e2dfb3c7bad0c0ea603d46771b88fe75539 Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Sat, 1 May 2021 22:21:31 +0800 Subject: [PATCH 2/4] feat: add javascript solution to lcci problem: No.02.02.Kth Node From End of List --- lcci/02.01.Remove Duplicate Node/README.md | 2 +- lcci/02.01.Remove Duplicate Node/README_EN.md | 2 +- .../02.02.Kth Node From End of List/README.md | 28 +++++++++++++++++++ .../README_EN.md | 28 +++++++++++++++++++ .../Solution.js | 23 +++++++++++++++ 5 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 lcci/02.02.Kth Node From End of List/Solution.js diff --git a/lcci/02.01.Remove Duplicate Node/README.md b/lcci/02.01.Remove Duplicate Node/README.md index 5a3a60855184e..b61141778b974 100644 --- a/lcci/02.01.Remove Duplicate Node/README.md +++ b/lcci/02.01.Remove Duplicate Node/README.md @@ -104,7 +104,7 @@ class Solution { ### **JavaScript** -```javascript +```js /** * Definition for singly-linked list. * function ListNode(val) { diff --git a/lcci/02.01.Remove Duplicate Node/README_EN.md b/lcci/02.01.Remove Duplicate Node/README_EN.md index d618886cf6087..95c927c1291ff 100644 --- a/lcci/02.01.Remove Duplicate Node/README_EN.md +++ b/lcci/02.01.Remove Duplicate Node/README_EN.md @@ -105,7 +105,7 @@ class Solution { ### **JavaScript** -```javascript +```js /** * Definition for singly-linked list. * function ListNode(val) { diff --git a/lcci/02.02.Kth Node From End of List/README.md b/lcci/02.02.Kth Node From End of List/README.md index abc4722c9aba6..dd414758a0531 100644 --- a/lcci/02.02.Kth Node From End of List/README.md +++ b/lcci/02.02.Kth Node From End of List/README.md @@ -77,6 +77,34 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} k + * @return {number} + */ +var kthToLast = function(head, k) { + let fast = head, slow = head; + for (let i = 0; i < k; i++) { + fast = fast.next; + } + while (fast != null) { + fast = fast.next; + slow = slow.next; + } + return slow.val; +}; +``` + ### **...** ``` diff --git a/lcci/02.02.Kth Node From End of List/README_EN.md b/lcci/02.02.Kth Node From End of List/README_EN.md index 464e16741d508..fcb95d747eff5 100644 --- a/lcci/02.02.Kth Node From End of List/README_EN.md +++ b/lcci/02.02.Kth Node From End of List/README_EN.md @@ -69,6 +69,34 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} k + * @return {number} + */ +var kthToLast = function(head, k) { + let fast = head, slow = head; + for (let i = 0; i < k; i++) { + fast = fast.next; + } + while (fast != null) { + fast = fast.next; + slow = slow.next; + } + return slow.val; +}; +``` + ### **...** ``` diff --git a/lcci/02.02.Kth Node From End of List/Solution.js b/lcci/02.02.Kth Node From End of List/Solution.js new file mode 100644 index 0000000000000..3fa82d9feaf83 --- /dev/null +++ b/lcci/02.02.Kth Node From End of List/Solution.js @@ -0,0 +1,23 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} k + * @return {number} + */ + var kthToLast = function(head, k) { + let fast = head, slow = head; + for (let i = 0; i < k; i++) { + fast = fast.next; + } + while (fast != null) { + fast = fast.next; + slow = slow.next; + } + return slow.val; +}; \ No newline at end of file From 413ccab4a4c1bd3eb32e46e2e5e88d0a17ac7130 Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Sat, 1 May 2021 23:10:45 +0800 Subject: [PATCH 3/4] feat: add javascript solution to lcci problem: No.02.03.Delete Middle Node --- lcci/02.03.Delete Middle Node/README.md | 20 ++++++++++++++++++++ lcci/02.03.Delete Middle Node/README_EN.md | 20 ++++++++++++++++++++ lcci/02.03.Delete Middle Node/Solution.js | 15 +++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 lcci/02.03.Delete Middle Node/Solution.js diff --git a/lcci/02.03.Delete Middle Node/README.md b/lcci/02.03.Delete Middle Node/README.md index 55c35c3498679..428d984c60b8b 100644 --- a/lcci/02.03.Delete Middle Node/README.md +++ b/lcci/02.03.Delete Middle Node/README.md @@ -66,6 +66,26 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} node + * @return {void} Do not return anything, modify node in-place instead. + */ + var deleteNode = function(node) { + node.val = node.next.val + node.next = node.next.next +}; +``` + ### **...** ``` diff --git a/lcci/02.03.Delete Middle Node/README_EN.md b/lcci/02.03.Delete Middle Node/README_EN.md index 5b34c7d6518d8..600112b3890f0 100644 --- a/lcci/02.03.Delete Middle Node/README_EN.md +++ b/lcci/02.03.Delete Middle Node/README_EN.md @@ -60,6 +60,26 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} node + * @return {void} Do not return anything, modify node in-place instead. + */ + var deleteNode = function(node) { + node.val = node.next.val + node.next = node.next.next +}; +``` + ### **...** ``` diff --git a/lcci/02.03.Delete Middle Node/Solution.js b/lcci/02.03.Delete Middle Node/Solution.js new file mode 100644 index 0000000000000..47ee0fe28e02c --- /dev/null +++ b/lcci/02.03.Delete Middle Node/Solution.js @@ -0,0 +1,15 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} node + * @return {void} Do not return anything, modify node in-place instead. + */ + var deleteNode = function(node) { + node.val = node.next.val + node.next = node.next.next +}; \ No newline at end of file From a0688b89e28dfbfa5f2b9d8eaf2c70ce2ad07dcf Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Sun, 2 May 2021 10:35:41 +0800 Subject: [PATCH 4/4] feat: add javascript solution to lcci problem: No.17.04.Missing Number --- lcci/17.04.Missing Number/README.md | 16 ++++++++++++++++ lcci/17.04.Missing Number/README_EN.md | 16 ++++++++++++++++ lcci/17.04.Missing Number/Solution.js | 11 +++++++++++ 3 files changed, 43 insertions(+) create mode 100644 lcci/17.04.Missing Number/Solution.js diff --git a/lcci/17.04.Missing Number/README.md b/lcci/17.04.Missing Number/README.md index bb254afa7d962..b138f2d25b27f 100644 --- a/lcci/17.04.Missing Number/README.md +++ b/lcci/17.04.Missing Number/README.md @@ -63,6 +63,22 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function(nums) { + let res; + for (let i = 0; i < nums.length; i++) { + res = res ^ nums[i] ^ (i + 1); + } + return res; +}; +``` + ### **...** ``` diff --git a/lcci/17.04.Missing Number/README_EN.md b/lcci/17.04.Missing Number/README_EN.md index 8d38e6e64b912..747e75482c33a 100644 --- a/lcci/17.04.Missing Number/README_EN.md +++ b/lcci/17.04.Missing Number/README_EN.md @@ -61,6 +61,22 @@ class Solution { } ``` +### **JavaScript** + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function(nums) { + let res; + for (let i = 0; i < nums.length; i++) { + res = res ^ nums[i] ^ (i + 1); + } + return res; +}; +``` + ### **...** ``` diff --git a/lcci/17.04.Missing Number/Solution.js b/lcci/17.04.Missing Number/Solution.js new file mode 100644 index 0000000000000..7d961678f2356 --- /dev/null +++ b/lcci/17.04.Missing Number/Solution.js @@ -0,0 +1,11 @@ +/** + * @param {number[]} nums + * @return {number} + */ + var missingNumber = function(nums) { + let res; + for (let i = 0; i < nums.length; i++) { + res = res ^ nums[i] ^ (i + 1); + } + return res; +}; \ No newline at end of file