LeetCode Solution, Medium, 61. Rotate List

輪替列表

·Jul 20, 2022·

Subscribe to my newsletter and never miss my upcoming articles

61. Rotate List

題目敘述

Given the `head` of a linked list, rotate the list to the right by `k` places.

Example 1:

``````Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]
``````

Example 2:

``````Input: head = [0,1,2], k = 4
Output: [2,0,1]
``````

Constraints:

• The number of nodes in the list is in the range `[0, 500]`.
• `-100 <= Node.val <= 100`
• `0 <= k <= 2 * 10**9`

解法解析

解法範例

Go

``````/**
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
*/
func rotateRight(head *ListNode, k int) *ListNode {
}

var n int = 1
for oldTail.Next != nil {
oldTail = oldTail.Next
n++
}

for i := 0; i < n-k%n-1; i++ {
newTail = newTail.Next
}
newTail.Next = nil
}
``````

JavaScript

``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {number} k
* @return {ListNode}
*/
const rotateRight = function (head, k) {
}

n = 1;
while (oldTail.next) {
oldTail = oldTail.next;
n++;
}

for (let i = 0; i < n - (k % n) - 1; i++) {
newTail = newTail.next;
}
newTail.next = null;
};
``````

Kotlin

``````/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* class ListNode(var `val`: Int) {
*     var next: ListNode? = null
* }
*/
class Solution {
fun rotateRight(head: ListNode?, k: Int): ListNode? {

var count: Int = 1
while (oldTail?.next != null) {
oldTail = oldTail.next
count++
}

for (i in 0 until count - k % count - 1) {
newTail = newTail?.next
}
newTail?.next = null
}
}
``````

PHP

``````/**
* Definition for a singly-linked list.
* class ListNode {
*     public \$val = 0;
*     public \$next = null;
*     function __construct(\$val = 0, \$next = null) {
*         \$this->val = \$val;
*         \$this->next = \$next;
*     }
* }
*/
class Solution
{

/**
* @param Integer \$k
* @return ListNode
*/
{
}

\$n = 1;
while (!is_null(\$oldTail->next)) {
\$oldTail = \$oldTail->next;
\$n++;
}

for (\$i = 0; \$i < \$n - \$k % \$n - 1; \$i++) {
\$newTail = \$newTail->next;
}
\$newTail->next = null;
}
}
``````

Python

``````# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
def rotateRight(self, head: ListNode | None, k: int) -> ListNode | None:
# base cases

# close the linked list into the ring
n: int = 1
while old_tail.next:
old_tail = old_tail.next
n += 1

# find new tail : (n - k % n - 1)th node
# and new head : (n - k % n)th node
for _ in range(n - k % n - 1):
new_tail = new_tail.next

# break the ring
new_tail.next = None

``````

Rust

``````
``````

Swift

``````/**
* public class ListNode {
*     public var val: Int
*     public var next: ListNode?
*     public init() { self.val = 0; self.next = nil; }
*     public init(_ val: Int) { self.val = val; self.next = nil; }
*     public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
* }
*/
class Solution {
func rotateRight(_ head: ListNode?, _ k: Int) -> ListNode? {
}

var n: Int = 1
while oldTail?.next != nil {
oldTail = oldTail?.next
n += 1
}

for _ in 0..<(n - k % n - 1) {
newTail = newTail?.next
}