diff --git a/solution/0000-0099/0061.Rotate List/README.md b/solution/0000-0099/0061.Rotate List/README.md index ac07bd1b8cf09..c2f07a38cd117 100644 --- a/solution/0000-0099/0061.Rotate List/README.md +++ b/solution/0000-0099/0061.Rotate List/README.md @@ -122,6 +122,48 @@ 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 rotateRight(head: ListNode | null, k: number): ListNode | null { + if (k == 0 || head == null || head.next == null) return head; + // mod n + let n = 0; + let p = head; + while (p != null) { + ++n; + p = p.next; + } + k %= n; + if (k == 0) return head; + + let fast = head, slow = head; + for (let i = 0; i < k; ++i) { + fast = fast.next; + } + while (fast.next != null) { + slow = slow.next; + fast = fast.next; + } + let start = slow.next; + slow.next = null; + fast.next = head; + return start; +}; +``` + ### **C#** ```cs diff --git a/solution/0000-0099/0061.Rotate List/README_EN.md b/solution/0000-0099/0061.Rotate List/README_EN.md index a285382b04576..33795df196b52 100644 --- a/solution/0000-0099/0061.Rotate List/README_EN.md +++ b/solution/0000-0099/0061.Rotate List/README_EN.md @@ -108,6 +108,48 @@ 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 rotateRight(head: ListNode | null, k: number): ListNode | null { + if (k == 0 || head == null || head.next == null) return head; + // mod n + let n = 0; + let p = head; + while (p != null) { + ++n; + p = p.next; + } + k %= n; + if (k == 0) return head; + + let fast = head, slow = head; + for (let i = 0; i < k; ++i) { + fast = fast.next; + } + while (fast.next != null) { + slow = slow.next; + fast = fast.next; + } + let start = slow.next; + slow.next = null; + fast.next = head; + return start; +}; +``` + ### **C#** ```cs diff --git a/solution/0000-0099/0061.Rotate List/Soution.ts b/solution/0000-0099/0061.Rotate List/Soution.ts new file mode 100644 index 0000000000000..91e4eceeae087 --- /dev/null +++ b/solution/0000-0099/0061.Rotate List/Soution.ts @@ -0,0 +1,37 @@ +/** + * 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 rotateRight(head: ListNode | null, k: number): ListNode | null { + if (k == 0 || head == null || head.next == null) return head; + // mod n + let n = 0; + let p = head; + while (p != null) { + ++n; + p = p.next; + } + k %= n; + if (k == 0) return head; + + let fast = head, slow = head; + for (let i = 0; i < k; ++i) { + fast = fast.next; + } + while (fast.next != null) { + slow = slow.next; + fast = fast.next; + } + let start = slow.next; + slow.next = null; + fast.next = head; + return start; +}; \ No newline at end of file