From cf550bac73c8fa200d682e361cc619f96f0bbd2d Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Wed, 17 Nov 2021 15:16:30 +0800 Subject: [PATCH] feat: add typescript solution to lc problem: No.2074.Reverse Nodes in Even Length Groups --- .../README.md | 43 +++++++++++++++++++ .../README_EN.md | 43 +++++++++++++++++++ .../Solution.ts | 38 ++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 solution/2000-2099/2074.Reverse Nodes in Even Length Groups/Solution.ts diff --git a/solution/2000-2099/2074.Reverse Nodes in Even Length Groups/README.md b/solution/2000-2099/2074.Reverse Nodes in Even Length Groups/README.md index 799313676c475..e5e53c6e4e08f 100644 --- a/solution/2000-2099/2074.Reverse Nodes in Even Length Groups/README.md +++ b/solution/2000-2099/2074.Reverse Nodes in Even Length Groups/README.md @@ -190,6 +190,49 @@ class Solution { } ``` +### **TypeScript** + +```ts +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +function reverseEvenLengthGroups(head: ListNode | null): ListNode | null { + let nums = []; + let cur = head; + while (cur) { + nums.push(cur.val); + cur = cur.next; + } + + const n = nums.length; + for (let i = 0, k = 1; i < n; i += k, k++) { + // 最后一组, 可能出现不足 + k = Math.min(n - i, k); + if (!(k & 1)) { + let tmp = nums.splice(i, k); + tmp.reverse(); + nums.splice(i, 0, ...tmp); + } + } + + cur = head; + for (let num of nums) { + cur.val = num; + cur = cur.next; + } + return head; +}; +``` + ### **...** ``` diff --git a/solution/2000-2099/2074.Reverse Nodes in Even Length Groups/README_EN.md b/solution/2000-2099/2074.Reverse Nodes in Even Length Groups/README_EN.md index 491d425f6d6fb..d4b78d3fe4f47 100644 --- a/solution/2000-2099/2074.Reverse Nodes in Even Length Groups/README_EN.md +++ b/solution/2000-2099/2074.Reverse Nodes in Even Length Groups/README_EN.md @@ -174,6 +174,49 @@ class Solution { } ``` +### **TypeScript** + +```ts +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +function reverseEvenLengthGroups(head: ListNode | null): ListNode | null { + let nums = []; + let cur = head; + while (cur) { + nums.push(cur.val); + cur = cur.next; + } + + const n = nums.length; + for (let i = 0, k = 1; i < n; i += k, k++) { + // 最后一组, 可能出现不足 + k = Math.min(n - i, k); + if (!(k & 1)) { + let tmp = nums.splice(i, k); + tmp.reverse(); + nums.splice(i, 0, ...tmp); + } + } + + cur = head; + for (let num of nums) { + cur.val = num; + cur = cur.next; + } + return head; +}; +``` + ### **...** ``` diff --git a/solution/2000-2099/2074.Reverse Nodes in Even Length Groups/Solution.ts b/solution/2000-2099/2074.Reverse Nodes in Even Length Groups/Solution.ts new file mode 100644 index 0000000000000..102cb44da0b7b --- /dev/null +++ b/solution/2000-2099/2074.Reverse Nodes in Even Length Groups/Solution.ts @@ -0,0 +1,38 @@ +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + + function reverseEvenLengthGroups(head: ListNode | null): ListNode | null { + let nums = []; + let cur = head; + while (cur) { + nums.push(cur.val); + cur = cur.next; + } + + const n = nums.length; + for (let i = 0, k = 1; i < n; i += k, k++) { + // 最后一组, 可能出现不足 + k = Math.min(n - i, k); + if (!(k & 1)) { + let tmp = nums.splice(i, k); + tmp.reverse(); + nums.splice(i, 0, ...tmp); + } + } + + cur = head; + for (let num of nums) { + cur.val = num; + cur = cur.next; + } + return head; +}; \ No newline at end of file