Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions solution/0000-0099/0061.Rotate List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions solution/0000-0099/0061.Rotate List/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions solution/0000-0099/0061.Rotate List/Soution.ts
Original file line number Diff line number Diff line change
@@ -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;
};